Prerequisites : Node, npm
Steps:
npm init
in the root folder. Fill up the details and it will generate a package.json for you. You may change the entry point to server.js, optional. Create a folder "backend" and inside it a file "server.js"Edit the package.json file and add in the scripts object, the following:
"start": "node backend/server.js",
"server": "nodemon backend/server.js"Install the dependencies:
npm i express dotenv
npm i -D nodemon
-D implies that it is a devDependency.
Lets Edit the server.js file now to spin up a server.
- Bring in express and other stuff by requiring it at the top.
- dotenv is used so that we can use Environment variables.
- Specify the
PORT=5000
in a .env file, that way we can acces it in the code likeprocess.env.PORT
- Make a instance of express, using its constructor and assign it to app
- Call the listen method on app object and pass in the port and the callback
Now, you can fire it up by running the command npm run server
, this way nodemon will watch our files for changes and server will be up and running.
Top comments (0)