Skip to content
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

Could json-server watch multiple files #434

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

Could json-server watch multiple files #434

snowffer opened this issue Dec 8, 2016 · 8 comments

Comments

@snowffer
Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link
Owner

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

This comment has been minimized.

Copy link

@zugarzeeker zugarzeeker commented Dec 8, 2016

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

@saibs

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@sehmaschine sehmaschine commented Feb 23, 2017

@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

This comment has been minimized.

Copy link

@stowball stowball commented Sep 4, 2017

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

@TryRaymond

This comment has been minimized.

Copy link

@TryRaymond TryRaymond commented Apr 9, 2019

@stowball

Why did you fail?

@TryRaymond

This comment has been minimized.

Copy link

@TryRaymond TryRaymond commented Apr 9, 2019

nice ,

use nodemon --watch mock --exec json-server mock/index.js --port 3020 --m mock/post-to-get.js is success !

you can use nodemon --watch mock --exec json-server mock/index.js --port 3020 ,

don't need '

@ saibs

but ,

I still need to press F5 ,

Otherwise, the page will not refresh actively

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

Successfully merging a pull request may close this issue.

None yet
7 participants
You can’t perform that action at this time.