Introduction
In this blog, we will be going through the basics of node Js and express and will see how we can create a server with the help of Express.
Node Js and Express
NodeJs is an open-source environment that lets you run javascript outside the browser. With the help of node JS, we can create backend services like APIs whereas Express is a free and open-source framework for node JS. Express was created to make APIs and web applications with ease with the help of node js.
So let’s get started with setting up the project.
Setting up the Project
To install node JS, you can go to this link and download it. After that, you can run the command
node -v
This command will give you the version of the node installed.
Now, first, we will create a directory
mkdir express-demo
Now after navigating to the directory we need to run this command.
npm init
After this, you will notice package.json file is created in the directory.
Now we will install express.
npm install express --save
Now you can see in the package.json, that express will be added as a dependency and a node modules folder will be created.
Creating a server with Express and Node
Now create a file app.js and write this code in it and let us understand it.
const express = require('express')
const app = express()
app.use((req, res, next) => {
res.setHeader('Content-Type', 'text/html');
next();
})
app.use((req, res, next) => {
res.send('<h1>Successfully Created Server</h1>');
})
app.listen(3000)
The first line is like importing the express module so that we can use its functionalities. Then we call express like a function so that the app object will now have all the functionalities provided by express.
Next, we create middleware. Now the question arises what is middleware?
Middlewares are basically functions that have access to the request object, response object, and the next function. When we call the next in a middleware, it indicates that execute the next middleware instead of stopping at this middleware only. These middlewares can change the request and response object and can call the next middleware in the stack.
To define a middleware, we use the app.use() function which in turn takes a function as a parameter and there we have a request, response, and next.
In the first middleware we have just set the headers and in the second middleware we are sending the response with the help of send function.
And at last, we call the listen function with the port on which the server will run.
Now let us run our server with this command
node app.js
And redirect to localhost:3000 and you will see the output as ‘Successfully Created Server’.

Conclusion
So, in this blog, we have gone through the introduction to node and express and how we can install it. Next, we went through how we can create a simple server with the help of express in node and run it. To learn more about express you can go through the official documentation here.
For more updates, please follow our LinkedIn page- FrontEnd Studio.
Thank you for sticking to the end. If you like the blog, please don’t forget to give a thumbs up and share it. Feel free to share your thoughts about this blog in the comments.