Express Server

Express Server

How to get going with the Express server, Quick and easy!

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

With a myriad of HTTP utility methods and middleware at your disposal, creating a robust API is quick and easy.

👉Prerequisites : Node, npm

👉 Steps:

  1. 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"

  2. Edit the package.json file and add in the scripts object, the following:
    "start": "node backend/server.js", "server": "nodemon backend/server.js"

  3. 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.

nodejs code for express 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 like process.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.