Skip to content

Could json-server watch multiple files #434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
snowffer opened this issue Dec 8, 2016 · 7 comments
Open

Could json-server watch multiple files #434

snowffer opened this issue Dec 8, 2016 · 7 comments

Comments

@snowffer
Copy link

snowffer commented Dec 8, 2016

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?

@zugarzeeker
Copy link

zugarzeeker commented Dec 8, 2016

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

@typicode
Copy link
Owner

typicode commented Dec 8, 2016

@zugarzeeker Nice implementation 👍
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"

@zugarzeeker
Copy link

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

@saibs
Copy link

saibs commented Jan 18, 2017

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

@sehmaschine
Copy link

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

@stowball
Copy link

stowball commented Sep 4, 2017

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

@PLQin
Copy link

PLQin commented Sep 9, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants
@sehmaschine @stowball @saibs @snowffer @typicode @zugarzeeker @PLQin and others