11

I often use the npm module json-server to generate a fake JSON api given a db.json file. Is there any way to deploy that to Heroku? Essentially, I just run

json-server --watch db.json

And it runs a server that also servers static html if there are any in the public/ directory. Essentially, I think it is just running a Node server, except I tried pushing to Heroku, but it did not work. Is there a special procedure I should do in order to make Heroku run that json-server module as my server?

nbkhope
  • 7,360
  • 4
  • 40
  • 58
  • 1
    Did you solve this? I'm trying to to the same for XHR mock backend – Adam Mendoza Dec 14 '16 at 04:16
  • 1
    @Adam, I actually did solve it. Here is the repository: https://github.com/nbkhope/fake-restful-api One thing I was not able to do was redirect the logging produced from json-server to the app. It would be great if anyone helped with that. Make a pull request and contribute! Thanks. – nbkhope Dec 22 '16 at 22:47
  • I don't see the login logic in the repo. I think the json-server can return a successful login status and the redirect would need to be done with something like jQuery unless you're using Angular or some other framework. If you update your repo I can take a look. – Adam Mendoza Dec 23 '16 at 04:58
  • @AdamMendoza I am not sure what you are saying. When I said logging, I meant console logs whenever a request is made to the server. Something displaying in the terminal saying GET /whatever was requested, etc. – nbkhope Dec 23 '16 at 18:24
  • I misread that as login vs. logging. I'll post update. – Adam Mendoza Dec 24 '16 at 22:16

3 Answers3

2

If you want to consume your fake APIs from an application published on Heroku, AWS Elastic Beanstalk etc., use My Json Server https://my-json-server.typicode.com/ . As far as I know, it is free for a small json.db file.

All you need to do is:

  1. Create a repository on gitHub and put your json.db there
  2. Call you API https://my-json-server.typicode.com/<your-github-username>/<your-github-repo>
irkForce
  • 352
  • 2
  • 12
2

Under the hood, Heroku will look for a startup-script, this is by default npm start so make sure you have that in your package.json.

Late to the party, but in case someone needs, just check out this repo, change the db.json to what you want, then follow step-by-step instructions to deploy to Heroku.

bonniss
  • 445
  • 5
  • 13
-4

You can use the following command to start json server

  `json-server --watch db.json -p 3001 -d 2000

Here -p is port This is commond to start local json server on port 2000

Rajneesh Shukla
  • 1,048
  • 13
  • 21