Create a simple web server in Node.js using Express
Dony M Varkey
Posted on May 17, 2023
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
$ 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.
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...
{
"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
We need to install express first.
To install express run:
$ 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
{
"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.
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
$ node index
You should see the following out put in the terminal.
Yay! π. We have created an express server.
Now if you try to navigate to http://localhost:3000/ you will see the following page.
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.
...
app.get('/', (req, res) => {
res.send("Hello, world!");
})
...
This is our final index.js
file.
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.
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 ππ
Posted on May 17, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
June 11, 2024