json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Could json-server watch multiple files

Open snowffer opened this issue 9 years ago • 7 comments

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?

snowffer avatar Dec 08 '16 06:12 snowffer

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]
});
// 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

{
  "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 avatar Dec 08 '16 10:12 zugarzeeker

@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 avatar Dec 08 '16 12:12 typicode

@typicode Thanks for your suggestion. It works with node-touch. 👍

zugarzeeker avatar Dec 08 '16 16:12 zugarzeeker

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

saibs avatar Jan 18 '17 15:01 saibs

@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.

sehmaschine avatar Feb 23 '17 14:02 sehmaschine

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

stowball avatar Sep 04 '17 10:09 stowball

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

PLQin avatar Sep 09 '20 12:09 PLQin