json-server
json-server copied to clipboard
"Oops, found / character in database property" when using module with custom routes
I got a routes.json
file that I use to add some aliases as explained here:
https://github.com/typicode/json-server#add-custom-routes
My routes.json
looks like this:
{
"/my/custom/route/*": "/$1"
}
and it works just fine if I run it using:
json-server db.json --routes routes.json
The problem is that, if I try to use it as a module, I got this error:
Error: Oops, found / character in database property '/my/custom/route/*'.
my server.js
looks like this:
const jsonServer = require('json-server');
const server = jsonServer.create();
const db = jsonServer.router('db.json');
const routes = jsonServer.router('routes.json');
const middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(db);
server.use(routes);
server.listen(3000, () => {
console.log('JSON Server is running');
});
What is the content of your db.json file?
Same problem
Same issue, I am facing
I'm facing the same problem. My workaround was to use the rewriter
within the jsonServer
instead of adding the routes file:
server.use(
jsonServer.rewriter({
"my/custom/route": "/my-database-resource",
})
);
See https://www.npmjs.com/package/json-server, at the Rewrite section