how to work with many json files together?
I have more than 10 json files in one project. Different file is using for different purpose, i don't want to join them into one file. How to work with all of them in json-server?
@bi-kai To me it sounds like you want to use your files like one file but that the concatenation is handled by json-server and not by you, is that assumption correct?
@simeg - doesn't sound like that to me. I have the same situation and I don't want a single file. I want each route to fetch a different JSON file
@simeg communications between frontend and backend with RESTful API in my APP, i mock each one with individual json file which named by RESTful interface name. i really donot want to join json files together.
Why don't you use something like:
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('./db.js')
var middlewares = jsonServer.defaults()
server.use(middlewares)
server.use(router)
server.listen(3000, function () {
console.log('JSON Server is running')
})
command line
node server.js
Hi, @Nilegfx this option seems user friendly, sadly this doesn't seem to work.
When starting node, this craches complaining about
SyntaxError: Malformed JSON in file: ./tata.js
Unexpected token 'v' at 1:1
var firstRoute = require('./jsonfile1.json');
Is there a way to provider a js file to jsonServer.router()?
Hi,
@agouriou I'm providing a .js file to the router in that way:
var jsonServer = require('json-server');
var server = jsonServer.create();
var router = jsonServer.router(require('./jsonServer.js')());
var middlewares = jsonServer.defaults();
server.use(middlewares);
server.use(router);
server.listen(5000, function () {
console.log('JSON Server is running');
});
The "jsonServer.js" is a file I've developed to read all my .json files from a specific folder and merge all of them into one.
Notice the fact that those js file should returns a json file.
I hope it helps you. If doesn't work, please, let me know.
Thanks @semagarcia , I used something like this.
@semagarcia i use your solution and it work very great. But i have one thing to ask? How can i make a fake data like using https://github.com/marak/Faker.js ? Thank you.
Below is peace of code worked for me in
- Routing dynamic resource to static content (let say images)
- Watching multiple db.json file like db-1.json, db-2.json and so on and getting accumulated response form common port
//db.js module.exports = function () { return Object.assign({}, require('./db-1.json'), require('./db-2.json'), require('./db-3.json') ); }
// router.js var jsonServer = require('json-server') var demodata = require('./data/db.js')() var server_port = 4000;
var router = jsonServer.router(demodata) var server = jsonServer.create()
/**
- All image related routing */ server.use(jsonServer.rewriter({ '/image/:id': '/assets/images/Img_1.png' }))
server.use(jsonServer.defaults(['./public'])) //for static files server.use(router)
server.listen(server_port, () => { console.log(''); console.log('** '); console.log(' Json Server is Up & Running at port ' + server_port + ' '); console.log(' **'); console.log(''); });
Now run 'node .js' http://localhost:4000/<resouce1fromReport1.json>/ http://localhost:4000/<resouce1fromReport2.json>/ http://localhost:4000/image/1 will open image as response
Note: Do not trust much command line instruction like
- --static
- --route
- --configuration
Better to give try with 'JS' file. above instruction killed my 2 days.
Feel free for any doubt and clarification
How to pass server instance to routes.js so that we refer server.get('route', someapi); inside routes.js or any close solution to it?
@semagarcia @agouriou First of all thanks for the solution, But for me after doing the post or put operations it is not updating ( persisting ) the data to json file, is there something i am missing here?
@semagarcia @agouriou First of all thanks for the solution, But for me after doing the post or put operations it is not updating ( persisting ) the data to json file, is there something i am missing here?
I am also facing the same issue. While posting a request it is not updating the corresponding .json file.
@semagarcia @agouriou First of all thanks for the solution, But for me after doing the post or put operations it is not updating ( persisting ) the data to json file, is there something i am missing here?
Everything works in memory and if you want to save data you need to press 's', this is how it should work if you do this manualy:
json-server db.json // run server with some data // Update some data using put, post ... s // click s to save data
now you will have to files: db.json // updated file with changes db-2131231231231.json // backup of previous version of file
Why don't you use something like:
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('./db.js') var middlewares = jsonServer.defaults() server.use(middlewares) server.use(router) server.listen(3000, function () { console.log('JSON Server is running') })command line
node server.js
Hey, thanks for this helpful answer but can you use a different id name with this? e.g the way mongo db uses _id
demodata
I did't get you so can you pls specify in depth ?
Why don't you use something like:
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('./db.js') var middlewares = jsonServer.defaults() server.use(middlewares) server.use(router) server.listen(3000, function () { console.log('JSON Server is running') })command line
node server.js
I already use this configuration but my problem here is when I import a JS file the POST request is not working. the request is success but only on browser upon checking my json file is not updating no post added it only updating on browser upon reload the server the reset to empty array bcuz the file.json is not updating