Json-server: Could json-server watch multiple files

Created on 8 Dec 2016  Β·  7Comments  Β·  Source: typicode/json-server

I have massive data and I don't want put them in the same file. So could I separate them into multiple files and watch them at the same time?

Most helpful comment

I'm using nodemon to restart and watch multiple files. So, I'm serving my mock files from mock folder.

"scripts": {
    "mock": "nodemon --watch mock --exec 'json-server --port 6002 mock/db.js --routes mock/routes.json'"
  }
module.exports = function() {
    return {
        users: require('./data/users'),
                companies: require('./data/companies')
    }
};

All 7 comments

I think this is not a good solution, but it is easy to implement.

I used onchange for watching directory that I will update.
And also using concurrently for running json-server and onchange at the same time.

npm install --save-dev onchange json-server concurrently

// index.js
const user = require('./src/user.json')
const article = require('./src/article.json')
module.exports = () => ({
  users: [user, user],
  articles: [article, article]
});

```js
// update.js
const fs = require('fs')
const text = fs.readFileSync('./index.js', 'utf-8')
fs.writeFileSync('./index.js', text, 'utf-8')

File structure

β”œβ”€β”€ index.js
β”œβ”€β”€ package.json
β”œβ”€β”€ src
β”‚Β Β  β”œβ”€β”€ article.json
β”‚Β Β  └── user.json
└── update.js

Setup scripts in `package.json`
```json
{
  "scripts": {
    "start": "concurrently --kill-others \"npm run start:server\" \"npm run update:watch\"",
    "start:server": "json-server --watch index.js",
    "update:watch": "onchange 'src/**/*.json' -- node update"
  }
}

@zugarzeeker Nice implementation :+1:
As an alternative, I've not tested it, but I think it would be possible to use https://github.com/isaacs/node-touch and do:

"update:watch": "onchange 'src/**/*.json' -- touch index.js"

@typicode Thanks for your suggestion.
It works with node-touch.
πŸ‘

I'm using nodemon to restart and watch multiple files. So, I'm serving my mock files from mock folder.

"scripts": {
    "mock": "nodemon --watch mock --exec 'json-server --port 6002 mock/db.js --routes mock/routes.json'"
  }
module.exports = function() {
    return {
        users: require('./data/users'),
                companies: require('./data/companies')
    }
};

@zugarzeeker Thanks for the solution. Though I'm not sure if your approach is supposed to work with POST requests ... is it? Because I'm able to to retrieve data, but I'm not able to update the json files. So before I dig deeper, I just wanted to make sure if this doesn't work by purpose.

None of these suggestions worked for me :( It really seems like this should be out-of-the-box functionality…

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()
const router = jsonServer.router(data)
const middlewares = jsonServer.defaults()

// 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 watching to all files

Was this page helpful?
0 / 5 - 0 ratings

Related issues

casvil picture casvil  Β·  4Comments

bahmutov picture bahmutov  Β·  3Comments

ashleydavis picture ashleydavis  Β·  3Comments

sboudouk picture sboudouk  Β·  3Comments

melnikovic picture melnikovic  Β·  3Comments