'can't watch multiple files with json-server
I've read about Fake json-server and I'd like to watch more than 1 file. In the instructions it is listed
--watch, -w
Watch file(s)
but I'm not able to make it working if I launch it as
json-server -w one.json two.json more.json
Solution 1:[1]
create files as shown below
db.js
var firstRoute = require('./jsonfile1.json');
var secondRoute = require('./jsonfile2.json');
var thirdRoute = require('./jsonfile3.json');
var fourthRoute = require('./jsonfile4.json');
// and so on
module.exports = function() {
return {
firstRoute : firstRoute,
secondRoute : secondRoute,
thirdRoute : thirdRoute,
fourthRoute : fourthRoute
// and so on
}
}
server.js
var jsonServer = require('json-server')
var server = jsonServer.create()
var router = jsonServer.router(require('./db.js')())
var middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
server.listen(3000, function () {
console.log('JSON Server is running')
})
Now go to the directory where you have created both these files and open command line and run the code below
node server.js
That's it now go to the browser and go to localhost:3000 ,You shall see the routes that created for different files,you may use it directly.
Solution 2:[2]
You can open multipe port for differents json files with json-server.In my case I open multiples cmd windows and launch it as.
json-server --watch one.json -p 4000
json-server --watch two.json -p 5000
json-server --watch more.json -p 6000
One for cmd window, this work for me.
Solution 3:[3]
It can only watch one file. You have to put the all info you need into the same file. So if you need cars
for one call and clients
for another, you would add a few objects from each into one file. It's unfortunate, but it's just supposed to be a very simple server.
Solution 4:[4]
1 - create database file e.g db.json
{
"products": [
{
"id": 1,
"name": "Caneta BIC Preta",
"price": 2500.5
},
],
"users":[
id:1,
name:"Derson Ussuale,
password: "test"
]
}
2 - In Package.json inside script
"scripts": {
"start": "json-server --watch db.json --port 3001"
},
3 - Finally Run command > npm start
Resources
http://localhost:3001/products
http://localhost:3001/users
Home
http://localhost:3001
Solution 5:[5]
You can do that with this things:
Step 1: Install concurrently
npm i concurrently --save-dev
Step 2: Create multiple json file, for an example db-users.json
, db-companies.json
and so on, and so on
Step 3: Add command line to your package.json
scripts, for an example:
servers: "concurrently --kill-others \"json-server --host 0.0.0.0 --watch db-users.json --port 3000\" \"json-server --host 0.0.0.0 --watch db-companies.json --port 3001\""
Step 4: Now you can run npm run servers
to run your multiple json file.
After that, you can access your server with: localhost:3000
and localhost:3001
or with your network ip address.
Note: You can add more files and more command into your package.json
scripts.
That's it.
Solution 6:[6]
As you can watch only one file simulteniously, because it is database, you can first read the database file and then append new data to database JSON:
const mockData = jsf(mockDataSchema);
const dataBaseFilePath = path.resolve(__dirname, {YOUR_DATABASE_FILE});
fs.readFile(dataBaseFilePath, (err, dbData) => {
const json = JSON.parse(dbData);
resultData = JSON.stringify(Object.assign(json, mockData));
fs.writeFile(dataBaseFilePath, resultData, (err) => {
if (err) {
return console.log(err);
}
return console.log('Mock data generated.);
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Neeraj Kamat |
Solution 2 | Takler21 |
Solution 3 | Travis Heeter |
Solution 4 | |
Solution 5 | Titus Sutio Fanpula |
Solution 6 | ????? ??????? |