Json-server: watch doesn't reload server

Created on 11 May 2017  路  7Comments  路  Source: typicode/json-server

// index.js
const casual = require('casual')

module.exports = () => {
  const data = { users: [] }
  // Create 1000 users
  for (let i = 0; i < 1000; i++) {
    data.users.push({ id: i, first_name: casual.first_name })
  }
  return data
}

node_modules/.bin/json-server -w index.js

I'd expect my server to reload when index.js is changed. It doesn't.

What's the purpose of watch? Or does it only work with json?

Most helpful comment

This problem still persists today. Will this ever be fixed?

All 7 comments

Same issue here. Can't get the server to reload db or at least restart the whole server (address in use). It's very time consuming to restart every npm process during the development with CTRL-C.

Seeing something similar here myself.
It is working fine if I snapshot the db and watch the snapshot json file but when watching the original .js file I am not getting a reload.

Me too

This problem still persists today. Will this ever be fixed?

hopefully they fix this.., Or can anyone suggest the alternative of this.

Just noticed people are still having problems with this.

You can always bypass specific lib's non-reloading watchers by doing something like

{
  "scripts": {
    "dev": "nodemon -w some-dir -x node server.js" // watch some directory, when something changes, eXecute `$ node server.js`
  },
  "devDependencies": {
    "nodemon": "latest"
  }
}

You'll encounter 50,000 libraries that do this wrong. Get cozy with a workaround like nodemon -x.

use nodemon 馃憤

start.js :

// github.com/remy/nodemon
var nodemon = require('nodemon');
nodemon({
  script: 'index.js',
  ext: 'js json' // watching extensions
});

nodemon.on('start', function () {
  console.log('App has started');
}).on('quit', function () {
  console.log('App has quit');
  process.exit();
}).on('restart', function (files) {
  console.log('App restarted due to: ', files);
});

index.js :

const jsonServer = require('json-server')

const data = require('./api/db.js')
const routes = require('./api/routes.json')

const server = jsonServer.create()       // Express server
const router = jsonServer.router(data) // Express router
const middlewares = jsonServer.defaults()

// https://github.com/typicode/json-server/issues/690#issuecomment-348616467
// json-server options.bodyParser defalut is true
// server.use(jsonServer.bodyParser);

server.use(middlewares)
server.use(jsonServer.rewriter(routes))
server.use(router)

// Avoid CORS issue
// json-server options.noCors defalut is false
// server.use( (req, res, next) => {
//   res.header("Access-Control-Allow-Origin", "*");
//   // res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//   next();
// });

server.listen(9538, () => {
  console.log('JSON Server is running, see http://localhost:9538')
})

All you need to do is exec the command node start.js to listen to all files

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dotmobo picture dotmobo  路  4Comments

sboudouk picture sboudouk  路  3Comments

fishenal picture fishenal  路  3Comments

strom picture strom  路  4Comments

bahmutov picture bahmutov  路  3Comments