Creating a simple server using node and express

This article is the third and last part of the “Creating a server using node and express” series.
Check out other articles in this series:
- Part 1: Installing the required tools and software(Node, VS Code, Postman, Git).
- Part 2: Initialize project with npm init in node
- Part 3: Creating a server using node and express
Intro:
In the previous article, we initialized our project in VS Code. Now, it's time to finally start building our server.
Requirements: Level — beginner
- Internet connection
- Javascript basics
- Computer
What we will be doing?
- Download the package dependencies
- Create the index file which will contain the crux of the code.
- Run the server on port 8000.
Download Dependencies:
Dependencies, as the name suggests, are external packages (pieces of code) used to develop one’s own program.
In the vs code terminal enter the following command:
npm i express
- npm: Node package manager
- i: Short for install
- express: Name of the package to be installed

This will download the “express” package.
After the download is complete go to package.json to check whether the package has been installed.

As you can see in the image above you can see express along with its version which is 4.17.3 in the dependencies sections.
Also, on the right sidebar, one new file (package-lock.json) and a folder (node_modules) has been created.
- package-lock.json: A file to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers.
- node_modules: All the downloaded packages needed for the application are stored in this file.
What is express js?
Express js is an open-source web framework used to create APIs and web servers with minimal overhead. Basically, express js makes creating web APIs extremely easy in Node js.
Writing the code:
With the only dependency downloaded, we will start building our server.
In your VS Code project folder, create a new file called index.js. This file will be empty.
Note: “.js” is the extension that indicates that “index” is a javascript file.

I’ll be using Github gists to host code for further instructions. You may continue on VS Code
Import the express module in your index file.
- Here we are referencing the “express” module in a variable called express.
- By doing this we can access all the methods and functions which are exported from “express”
Initialize an instance of express
- We initialize an instance of the express module called “app” to use in building our application.

Include the port number on which you want your server to run
- Our server will listen to changes on this port number which is 8000.

Finally, let the express instance listen on the specified port number.
- The .listen() function is used to bind and listen to the connections on the specified host and port (8000).
- You can imagine .listen() as the Air Traffic Controller of an airport that listens to all the traffic coming in and out of an airport.

Running the server:
Finally, enter “node index” in the command terminal to start the server. If the server successfully starts you’ll see “Server started on port 8000” in the command terminal.
- node index: Command to tell node js to run the file “index.js”.
- In node js, it is not mandatory to use the “.js” when running or importing a file.

Cheers! You just created your first simple server!
This may seem basic but the applications of this simple piece of code are extremely wide-ranging!
Check out this repl to get the code of the entire project:
Conclusion:
In this article, we created and ran a simple server in node js using express.
Until next time!
Sources: