Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

404 on home page if public directory exists in the root of the project #1100

Open
myshov opened this issue Mar 10, 2020 · 6 comments
Open

404 on home page if public directory exists in the root of the project #1100

myshov opened this issue Mar 10, 2020 · 6 comments

Comments

@myshov
Copy link

myshov commented Mar 10, 2020

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:

  1. install json-server into fresh npm project
  2. create public directory in the root of this new project
  3. create db.json
  4. start npx json-server db.json
  5. check http://localhost:3000/

Expected behavior: home page should work
Actual behavior: home page doesn't work (404 error)

@myshov
Copy link
Author

myshov commented Mar 10, 2020

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:

const userDir = path.join(process.cwd(), 'public')
const defaultDir = path.join(__dirname, '../../public')
const staticDir = fs.existsSync(userDir) ? userDir : defaultDir
opts = Object.assign({ logger: true, static: staticDir }, opts)

@myshov
Copy link
Author

myshov commented Mar 10, 2020

@typicode hi! What do you think about removing default public fallback?

@myshov myshov changed the title 404 for home page if public directory exists in the root of the project 404 on home page if public directory exists in the root of the project Mar 10, 2020
@myshov
Copy link
Author

myshov commented Mar 10, 2020

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.

@leandrogehlen
Copy link

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

@afronorana
Copy link

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'"
}

@PLQin
Copy link

PLQin commented Sep 9, 2020

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 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants