Json-server: Watch when using the module

Created on 12 Jul 2016  路  2Comments  路  Source: typicode/json-server

Is there any way to combine watch with module usage?

Most helpful comment

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

All 2 comments

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)
    }
})
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jasonlimantoro picture jasonlimantoro  路  4Comments

ashleydavis picture ashleydavis  路  3Comments

bahmutov picture bahmutov  路  3Comments

pantchox picture pantchox  路  3Comments

AdamCook44 picture AdamCook44  路  3Comments