Json-server: can't watch multiple files

Created on 25 Apr 2016  路  13Comments  路  Source: typicode/json-server

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

Could you help me, please? It is really important

Most helpful comment

Did a quick test, it works.

Regarding the previous error:

Can't find module '/one.json'.

I would check that there isn't a typo like require('/one.json') instead of require('./one.json').

For reference and in case someone else needs it, here's the full code that I've used:

// db.js
module.exports = function() {
  return {
    one: require('./one.json'),
    two: require('./two.json')
  }
}

./one.json

[
  { "id": 1, "title": "foo" }
]

./two.json

[
  { "id": 1, "title": "bar" }
]
$ json-server db.js

All 13 comments

Hi @Jimmy5nomana,

The description may not be very clear. You can watch db.json file and the optional routes file (that's why there is a (s)). But json-server only accepts one "database" file and one routes file.

So you need to have one.json, two.json and more.json content in a single file. Do you need to use multiple files for a particular reason?

Ok, I'm in hurry for a corporate test, and I need to load a json file in a <div>. I have 4 files and all of them grouped in a directory.

It seems to me routes file let me read certain values of json object.
I need to load every json file at request, I can't put them together in a single file.

Thanks! Help me please!
P.S. i've read about json-server, Multiple Files but I can't make it working

I can see another ways of doing this, but it has cons.

Create db.js:

module.exports = function() {
  return {
    one: require('./one.json'),
    two: require('./two.json')
  }
}
json-server db.js

Data won't be persisted and you can't watch for changes. But if it's a test, that's fine.

Or, as suggested previously, manually copy the content of the JSON files and create a db.json file. Unlike the db.js solution, If you restart your server, changed data won't be lost.

Sorry, I know it's not really the same as json-server -w one.json two.json more.json, but I hope it will helpful nonetheless.

I said I can't make it working, I'll explain you how I'm launching it.

folder: db.js, one.json, two.json

I run json-server db.js inside the folder

Am I doing something wrong?
Terminal says error at

two: require('./two.json')

Have you tried if it works?

My bad there was a typo in the previous code example (missing , just after require('./one.json')).
I've updated the example.

Thanks for the immediate reply, but it still can't work.

Module.js:327: throw err

Do you know if this works? Is it fine how I'm running it?

It's a little hard to tell. Can you copy paste the full error?

Can't find module '/one.json'

It should be helpful if you could run it on your machine, to know if it certainly works.

I'll repeat how I'm doing: I've all in one folder, and I launch json-server db.js within that folder.

FIXED: I was working on the wrong db.js, I tried with another on the upper position of the folder

THANKS ,THANKS, THANK YOU VERY MUCH !!!

Did a quick test, it works.

Regarding the previous error:

Can't find module '/one.json'.

I would check that there isn't a typo like require('/one.json') instead of require('./one.json').

For reference and in case someone else needs it, here's the full code that I've used:

// db.js
module.exports = function() {
  return {
    one: require('./one.json'),
    two: require('./two.json')
  }
}

./one.json

[
  { "id": 1, "title": "foo" }
]

./two.json

[
  { "id": 1, "title": "bar" }
]
$ json-server db.js

glad it worked :smile:

@typicode wonder if we can do something like follows. in db json. So this will enables dev's to do a pattern matching with the URL's

module.exports = [
    {
        urlPattern: /\/api\/v1_0\/client\/status/,
        redirect: '/mock/clients/clientstatus.json'
    },
 {
        urlPattern: /\/api\/v1_0\/client\/details/,
        redirect: '/mock/clients/clientdetails.json'
    },

@samithaf

You can't in db.json, but you can add routes:
https://github.com/typicode/json-server#add-routes

For example:

{
  "/api/v1_0/client/status": "/mock/clients/clientstatus.json"
}

And create in ./public/mock/clients/clientstatus.json.

Or even better:

{
  "/api/v1_0/client/status": "/status"
}

And add an entry status in db.json.

If it's not enough, you can use the project as a module:
https://github.com/typicode/json-server#module

Was this page helpful?
0 / 5 - 0 ratings