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:
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
}
}
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')
})
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
//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()
/**
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/
http://localhost:4000/
http://localhost:4000/image/1 will open image as response
Note: Do not trust much command line instruction like
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.
Most helpful comment
Why don't you use something like:
db.js
server.js
command line