can't watch multiple files with json-server
Asked Answered
P

8

8

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
Philipson answered 25/4, 2016 at 9:18 Comment(0)
A
9

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.

Akel answered 11/5, 2017 at 13:40 Comment(0)
F
9

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.

Fizgig answered 17/5, 2019 at 8:58 Comment(5)
Does this worked for you?, when I run call a route through URL it is not routing to the required path. Ex: when I call jsonfile2 in my API, actions are not triggered for the requested json.Janitress
var router = jsonServer.router(require('./db.js')()). , works...Tetrarch
POST & PUT won’t persist in this caseObservatory
github.com/typicode/json-server/issues/… source of answer. its polite to attribute where you found your codeMicroparasite
Multiple files It is the right answer for my case. with lodash module it works .Cliffordclift
T
2

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.

Thiourea answered 29/9, 2016 at 15:29 Comment(0)
C
2

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
Colettacolette answered 30/4, 2022 at 12:0 Comment(0)
O
0

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.

Obscene answered 11/4, 2022 at 16:4 Comment(0)
G
0

I think it's not possible. JSON Server obviously works with a singleton instance of a JSON file, so it can only write and read in a single operation.

When multiple files are used, regardless of the coding techniques used for that, Json Server needs to write to a single file. Therefore, other solutions will only work for read-only.

It's more useful to use a code to merge JSON into a single file, such as a temporary file, before launching JSON Server. After JSON Server shuts down, split this temporary file and overwrite each source.

Gettysburg answered 6/9, 2023 at 11:58 Comment(0)
C
0

I found a solution using json-server itself and it worked for me:

files structure

├── data
│   ├── index.js
│   ├── posts.json
│   └── users.json
├── package.json

index.js

const users = require('./users.json');
const posts = require('./posts.json');

module.exports = () => ({
  users: users,
  posts: posts
});

package.json

...
"scripts": {
    "start": "json-server --watch ./data/index.js --port 3000"
}
...

ref: json-server-multiple-files

Covenantor answered 11/6, 2024 at 11:54 Comment(0)
A
-1

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.);
  });
});
Arrearage answered 11/6, 2018 at 12:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.