Create Express Server in NodeJS





Hey guys in this post, we will discuss about creating Express Server in Node.js with just 5 simple steps.

What is Express?


Express.js or simply Express is a light-weight web application framework to help organize your web application into an MVC architecture on the server side. We can use a variety of choices for our templating language (like EJS, Jade, and Dust.js). It is designed for building REST APIs. It has been called the de facto standard server framework for Node.js.

Steps to Create Express Server


Following are the steps to create Express Server in Node.js

Create empty directory


Open the command prompt and enter the following command to create empty directory

mkdir web-servers

Initialize the npm


On the root of our directory, open the command prompt and enter the following command to initialize the npm

npm init -y

This will generate package.json file on the root of your directory, which contains the app information and dependencies.

{
  "name": "web-servers",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Install Express


On the root of our directory, open the command prompt and enter the following command to install Express

npm i express

This will install the Express and added to the dependencies section inside the package.json file

Create Express Server


On the root of our directory, create a file app.js and add the following code

const express = require('express');

const app = express();

app.get('', (req, res) => {
    res.send('<h1>Hello Express!</h1>');
})

app.listen(3000, () => {
    console.log('Server is up and running');
})
  • At the top we will load the library express using require() method
  • We will create the express server using express() method
  • We will return <h1> content to the default route
  • We will start the express server by calling listen() method

Test the app


Open the browser and navigate to the URL localhost:3000, we will see the following text

Screenshot-2021-08-28-at-9-26-38-PM
That’s it for this post, if you like this post, share this with your friends and colleagues or you can share this within your social media platform. Thanks, I will see you in our next post.



About the author

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *