Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't watch multiple files #265

Closed
JimmyD5 opened this issue Apr 25, 2016 · 14 comments
Closed

can't watch multiple files #265

JimmyD5 opened this issue Apr 25, 2016 · 14 comments

Comments

@JimmyD5
Copy link

JimmyD5 commented Apr 25, 2016

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

@typicode
Copy link
Owner

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?

@JimmyD5
Copy link
Author

JimmyD5 commented Apr 25, 2016

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

@typicode
Copy link
Owner

typicode commented Apr 25, 2016

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.

@JimmyD5
Copy link
Author

JimmyD5 commented Apr 25, 2016

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?

@typicode
Copy link
Owner

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

@JimmyD5
Copy link
Author

JimmyD5 commented Apr 25, 2016

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?

@typicode
Copy link
Owner

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

@JimmyD5
Copy link
Author

JimmyD5 commented Apr 25, 2016

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 !!!

@JimmyD5 JimmyD5 closed this as completed Apr 25, 2016
@typicode
Copy link
Owner

typicode commented Apr 25, 2016

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

@typicode
Copy link
Owner

glad it worked 😄

@samithaf
Copy link

samithaf commented May 2, 2016

@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'
    },

@typicode
Copy link
Owner

typicode commented May 2, 2016

@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

@bdrazen
Copy link

bdrazen commented Mar 4, 2020

@TryRaymond Specifically #434 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@samithaf @typicode @bdrazen @JimmyD5 and others