If there is public directory in the root of the project then json-server attempts to serve index.html from there, as result the home page is not working.
Steps for reproduction of the bug:
public directory in the root of this new projectnpx json-server db.jsonhttp://localhost:3000/Expected behavior: home page should work
Actual behavior: home page doesn't work (404 error)
One possible fix is to delete default static fallback into public directory and suggest users always use --static option for serving static files.
Here is part of the code that cause this issue:
https://github.com/typicode/json-server/blob/622400f7ead64f02e626a7eaa38035fd1438b835/src/server/defaults.js#L11-L15
@typicode hi! What do you think about removing default public fallback?
I figured the better solution is to separate creation of users static server from static server that is used for serving json-server related stuff.
My solution :
const fs = require('fs');
const path = require('path');
const server = require('json-server');
const routes = require('./routes');
const app = server.create();
const middlewares = server.defaults({
static: path.join(__dirname, '../node_modules/json-server/public')
});
My solution :
const fs = require('fs'); const path = require('path'); const server = require('json-server'); const routes = require('./routes'); const app = server.create(); const middlewares = server.defaults({ static: path.join(__dirname, '../node_modules/json-server/public') });
Or in my case, I added the static route on the config file json-server.json;
{
"port": 3000,
"static": "'./node_modules/json-server/public'"
}
When I change the static ,
the page that json-server opens no longer shows' You're running JSON Server , but instead becomes a blank page .
Why is that ?
Most helpful comment
My solution :