// 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?
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.
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
Most helpful comment
This problem still persists today. Will this ever be fixed?