Creating a simple server using node and express

Photo by Taylor Vick on Unsplash

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:

  1. Part 1: Installing the required tools and software(Node, VS Code, Postman, Git).
  2. Part 2: Initialize project with npm init in node
  3. 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

  1. Internet connection
  2. Javascript basics
  3. Computer

What we will be doing?

  1. Download the package dependencies
  2. Create the index file which will contain the crux of the code.
  3. 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.

  1. 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.
  2. 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:

  1. Node js docs
  2. Express js docs

--

--

--

Hi, I'm Aunsh, an engineer, teacher and a self-taught web developer. I dabble in JS, React, Node, Postgres, Mongo and SQL to make interesting projects.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

D3.js — What’s new in Version 6?

Testing React Applications

Everything I learned from #Javascript30

Masai Unit-3 Construct Week Project

Quick React Native Hooks basics

A tiny event-based state manager Storeon for Corvid.

Build Auth App using AWS Cognito with NuxtJS and TypeScript

Switching an NX repo to PNPM

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aunsh Bandivadekar

Aunsh Bandivadekar

Hi, I'm Aunsh, an engineer, teacher and a self-taught web developer. I dabble in JS, React, Node, Postgres, Mongo and SQL to make interesting projects.

More from Medium

Initialize a project in node with npm init

Upload, Compress and Delete Images In a AWS S3 Bucket With Node.JS + Express

Create events on Google calendar using Google Calendar API in Node.js

Google Drive API, Resumable upload — Javascript