json-server icon indicating copy to clipboard operation
json-server copied to clipboard

how to work with many json files together?

Open bi-kai opened this issue 10 years ago • 16 comments

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 avatar Sep 26 '16 08:09 bi-kai

@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 avatar Oct 18 '16 20:10 simeg

@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

yairEO avatar Feb 12 '17 13:02 yairEO

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

bi-kai avatar Feb 21 '17 08:02 bi-kai

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

Nilegfx avatar Feb 22 '17 00:02 Nilegfx

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()?

agouriou avatar Mar 02 '17 10:03 agouriou

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.

semagarcia avatar Mar 23 '17 13:03 semagarcia

Thanks @semagarcia , I used something like this.

agouriou avatar Apr 24 '17 12:04 agouriou

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

ducpt2 avatar Jul 29 '18 13:07 ducpt2

Below is peace of code worked for me in

  1. Routing dynamic resource to static content (let say images)
  2. 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

  1. --static
  2. --route
  3. --configuration

Better to give try with 'JS' file. above instruction killed my 2 days.

Feel free for any doubt and clarification

vickyru avatar Feb 14 '19 11:02 vickyru

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?

hansiemithun avatar Feb 27 '19 09:02 hansiemithun

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

riyazmulla51 avatar Mar 31 '20 06:03 riyazmulla51

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

shubham0503 avatar Sep 07 '20 05:09 shubham0503

@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

vukan71 avatar Sep 09 '21 11:09 vukan71

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

coulsptr avatar Dec 29 '21 03:12 coulsptr

demodata

I did't get you so can you pls specify in depth ?

femil-socilpilot avatar Nov 04 '22 07:11 femil-socilpilot

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

RyaiStudio avatar Feb 18 '23 19:02 RyaiStudio