Getting Started with Node.js + Express

Ravi Singh
3 min readJun 4, 2023

--

Express + Nodejs

Node.js is a javascript runtime that is built on top of Chrome’s V8 Engine. It is very popular and used in various big tech companies.

Today we will be going to learn how can we create a simple web server in Node.js using one of the most popular libraries which is Express.

I believe that you already have Node installed on your machine. If not just download it from their official website. Also, I want you to have a basic knowledge of Javascript.

First thing first. Open the terminal/cmd on the machine.

Create an empty dir and create a package.json file by running:

mkdir server
cd server
npm init --yes

By running npm init --yes will create a package.json file. This file is being used as a config file for your server.

So, now we have a config file setup. Let’s install Express as a dependency in our project.

npm i express

By running the above command the npm which is a package manager for node, will install express as a dependency which we can use in our code. In order to open on vs code just run the bellow cmd on your project dir.

code .

We will be going to use modular-based imports in our project. so make sure we have our type set to module in our package.json file.

// package.json
{
...
...
"type": "module"
...
...
}

Now create a new file index.js and open it on your Editor/IDE. I prefer visual studio code you can use any.

# This will create an empty file.
touch index.js

Here we import express and store our express app by calling it.

// index.js
import express from 'express'

const app = express()

and now we just need to bind on a PORT where we can actually access our web server.

const PORT = 5000
app.listen(PORT, () => console.log(`Running at ${PORT}.`)

For now, we are assigning a static PORT but on a real-world application, this will be assigned by the machine on runtime but it’s fine to use a static for learning purposes for now just to make things easy.

Congratulations!! 🙌 🎉 You have successfully created a web server 😎.

Let's run our server and check out our browser. In Terminal/CMD run.

node index.js

This should print our success message which we have provided on callback earlier.

server run status

Let's open the Browser and access our web server from there. So in the address bar open http://localhost:5000

You will see something like this. Don’t worry if you see this. it means our server is running fine but we have not registered any route with / .

Let’s register some routes. So go back to index.js add some routes

app.get('/', (request, response) => {
response.send('Hi 😃!')
})

Let's restart the server and check again by reloading the tab. you should be able to see your response on the browser.

Hope you have got some brief knowledge of how we can create a simple web server in Nodejs using express.

--

--

Ravi Singh
Ravi Singh

Written by Ravi Singh

I'm a Software Engineer. Follow me on github: https://github.com/raysk4ever

No responses yet