Is there any way to combine watch with module usage?
Hi @kurtextrem,
Not right now, but it's something I'm considering.
As a workaround, I think you can do it using nodemon:
nodemon index.js --watch db.json
or
// db.js
module.exports = {
posts: []
}
// index.js
const db = require('./db')
// The router can accept an object but the side effect is that data won't be persisted
jsonServer.router(db)
nodemon index.js
I've solved it using your code:
chokidar
.watch(config.db)
.on('change', function (file) {
if (file === config.db) {
var obj = JSON.parse(fs.readFileSync(file))
console.info('Setting new DB state')
router.db.setState(obj)
}
})
Most helpful comment
I've solved it using your code: