Create a simple web server in Node.js using Express
Tags: javascript, beginners, tutorial, node
Published: 2023-05-17 10:15:55

In this article we will be creating a basic web server with a simple route to display **"Hello, World"**. 
Before we get started, you should have some knowledge in **javascript** and **npm**. 

> 
_Before getting started you should have **node** and **npm** installed in you system._

## Topics
- Introduction to Node.js
- Creating a Node.js server
- Testing the server
- Creating a route
- Testing the route

### Introduction to Node.js
Node.js is a asynchronous event driven javascript runtime that is based on chrome's V8 javascript engine. This helps us to run javascript on the server. It is single threaded hence cannot take use of the multiple cores that are available in the system.

### Creating a Node.js server

```bash
$ mkdir simple-node-js-server
$ cd simple-node-js-server
$ npm init -y
$ code .
```
`npm init -y` initializes a node.js project.
`code .` will open the Visual Studio Code in the project directory (Assuming that you have VS Code installed.)

After opening VS Code, you should see a package.json file in the project directory.


![Project Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/166pnd3g7rgjn6cc8vkn.png)
 

> _The package.json file contains different metadata about the project, and helps to handle project dependencies._

This is how a package.json file looks like...

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

```

Now let's get into the important stuff.
Create an entry point to our server in the root of our project directory. We name it `index.js`.

> _**Note:** The entry point file name should be same as that of `main` in the package.json file_



![index.js](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jigzvbzdozp7kp7tpdt8.png)

We need to install express first.
To install express run:
```bash
$ npm install express
```
> Express is a simple and minimal web application framework for Node.js.

After installing express, open the package.json file. You will find a dependencies object which will contain the dependencies of the project. In our case Express

```json
{
  "name": "simple-node-js-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1"
  }
}

```
> _To install any package run `npm install package_name`_

You will also find a node_modules directory which contains all of our dependencies. Now open the `index.js` file and paste the following contents in it.

```js
const express = require('express'); // importing the express dependency
const app = express(); // initialising the server

const PORT = 3000; // Port number on which our server will listen on

app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
}) // Telling the app to listen on the PORT 3000
```
### Testing the server

Now let's start the server. To start the server, run
```bash
$ node index
``` 
You should see the following out put in the terminal.

![Console output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iybe43sl6xfbz5oh2tpu.png)

Yay! 👏. We have created an express server.
Now if you try to navigate to http://localhost:3000/ you will see the following page.

![localhost](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngrij42nf8g88q9wjds3.png)

This is because you haven't defined any routes. Stop the running server using `CTRL+C`.

### Create a route
Now let's create an route. To create a route you have to add the following to the `index.js` file.
```js
...

app.get('/', (req, res) => {
    res.send("Hello, world!");
})

...
```
This is our final `index.js` file.
```js
const express = require('express'); 
const app = express();

const PORT = 3000;

app.get('/', (req, res) => {
    res.send("Hello, world!");
})

app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
})
```
### Testing the route
Run the server again using `node index`.
Now go to http://localhost:3000. You shall see the following.

![route](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpvd49gi8wsiybf7vx0u.png)

And there you go 😀. We have a created a web server with a single route. With this it's a wrap for this article. Let me know your thoughts in the comments. Have a nice day 👋👋