Here you can see how to deploy you mock-server(db.json) file to heroku.
- Create a folder and name it any name or fake_server.
- Create a file and make the entry point server.js
- Open the folder in editor and open terminal and let's Start
npm init
npm i json-server
- Add a
start
script inpackage.json
- Your
package.json
should look like this or you can paste this code in yourpackage.json
{
"name": "fake-server",
"version": "1.0.0",
"description": "fake server with fake database",
"main": "server.js",
"scripts": { // <===
"start": "node server.js" // <===
},
"author": "Akshay Kumar",
"license": "ISC",
"dependencies": {
"json-server": "^0.16.3"
}
}
- create
.gitignore
file and addnode_modules
.gitignore
- Open
server.js
file and paste the following code
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json'); // file name of your database
const middlewares = jsonServer.defaults();
const port = process.env.PORT || 4000; // You can change the port
server.use(middlewares);
server.use(router);
server.listen(port);
- Create db.json file Fill in any data you like, or use Mockaroo which is a great and easy way to generate dummy data.
{
"users": [
{
"id": 1,
"first_name": "Justina",
"last_name": "Ginglell",
"email": "[email protected]",
"gender": "Female"
},
{
"id": 2,
"first_name": "Marion",
"last_name": "Jenman",
"email": "[email protected]",
"gender": "Male"
},
{
"id": 3,
"first_name": "Alfy",
"last_name": "Begin",
"email": "[email protected]",
"gender": "Female"
},
{
"id": 4,
"first_name": "Karney",
"last_name": "Zanussii",
"email": "[email protected]",
"gender": "Male"
},
{
"id": 5,
"first_name": "Reid",
"last_name": "Schapero",
"email": "[email protected]",
"gender": "Male"
},
{
"id": 6,
"first_name": "Dorine",
"last_name": "Braybrookes",
"email": "[email protected]",
"gender": "Female"
},
{
"id": 7,
"first_name": "Sarena",
"last_name": "Frape",
"email": "[email protected]",
"gender": "Female"
},
{
"id": 8,
"first_name": "Malva",
"last_name": "Pierse",
"email": "[email protected]",
"gender": "Female"
},
{
"id": 9,
"first_name": "Rania",
"last_name": "Dablin",
"email": "[email protected]",
"gender": "Female"
},
{
"id": 10,
"first_name": "Ingrim",
"last_name": "Offen",
"email": "[email protected]",
"gender": "Male"
}
]
}
Heroku is a free hosting service for hosting small projects. Easy setup and deploy from the command line via git.
- Easy setup
- Free
- App has to sleep a couple of hours every day.
- "Powers down" after 30 mins of inactivity. Starts back up when you visit the site but it takes a few extra seconds. Can maybe be solved with Kaffeine
2 . Create an account on
https://heroku.com
3 . Install the Heroku CLI on your computer:
https://devcenter.heroku.com/articles/heroku-cli
4 . Connect the Heroku CLI to your account by writing the following command in your terminal and follow the instructions on the command line:
heroku login
5 . Then create a remote heroku project, kinda like creating a git repository on GitHub. This will create a project on Heroku with a random name. If you want to name your app you have to supply your own name like heroku create project-name
:
heroku create my-cool-project
6 . Push your app to Heroku (you will see a wall of code)
git push heroku master
7 . Visit your newly create app by opening it via heroku:
heroku open
8 . For debugging if something went wrong:
heroku logs --tail
Heroku will look for a startup-script, this is by default npm start
so make sure you have that in your package.json
(assuming your script is called server.js
):
"scripts": {
"start" : "node server.js"
}
You also have to make changes to the port, you can't hardcode a dev-port. But you can reference herokus port. So the code will have the following:
const port = process.env.PORT || 4000;