Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't watch multiple files with json-server

Tags:

json

rest

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
like image 454
FrancescoN Avatar asked Apr 25 '16 09:04

FrancescoN


2 Answers

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.

like image 107
Neeraj Kamat Avatar answered Oct 12 '22 04:10

Neeraj Kamat


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.

like image 38
Takler21 Avatar answered Oct 12 '22 05:10

Takler21