json-server icon indicating copy to clipboard operation
json-server copied to clipboard

"Oops, found / character in database property" when using module with custom routes

Open FrancescoBorzi opened this issue 7 years ago • 4 comments

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');
});

FrancescoBorzi avatar Oct 15 '17 11:10 FrancescoBorzi

What is the content of your db.json file?

nabarunchakma avatar Nov 27 '17 13:11 nabarunchakma

Same problem

alxpsr avatar May 27 '18 10:05 alxpsr

Same issue, I am facing

ajaynarang avatar Jan 22 '20 11:01 ajaynarang

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

camilodsilva avatar Dec 07 '21 13:12 camilodsilva