Learn Node.js from scratch and create your first web server with Express

Rajesh Bhattarai
3 min readJul 30, 2020
Photo by Hitesh Choudhary on Unsplash

In this article, you will learn What is Node.js? What are the benefits? And you will create your very own first webserver.

What is Node.js?

According to Wikipedia, Node.js is an open-source, cross-platform, JavaScript runtime environment (Framework) that executes JavaScript code outside a web browser. Again some regular Buzzwords and you end up understanding nothing, I know. In simple terms, Node.js helps you to build a highly scalable server-side application using JavaScript. Using Node.js you can build a command-line application, web application, real-time chat application, and web servers.

Benefits of Node.js

Node.js is easy to learn once you learn Javascript.
Node.js is an open-source framework under the MIT license.
It helps you create high performing web servers.
It is cross-platform (runs on Windows, Mac, and Linux).
You can build easy to scale application.

Let’s start and create our first web server step by step

Photo by Marvin Meyer on Unsplash

Install Node.js

If you haven’t installed the Node.js then download the Node.js from the official website.

Creating a Package.json file

After installing Node.js on your system, the second step is to create a package.json file.

Create a working directory(create a folder with the name “mywebserver”) of your application. Open your terminal and go to the directory you have created and run a command “npm init”. Number of things including name, version, entry point file (by default this is index.js) will be asked, just accept the defaults. It will create a package.json file. package.json is a plain JSON(JavaScript Object Notation) text file which contains all metadata information about your Node.js application.

{
“name”: “mywebserver”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“author”: “”,
“license”: “ISC”
}

Install Express.js

ExpressJS is a web application framework that provides a minimal interface to build our applications. It helps you build a web server in the most minimal and simple format as possible.

Now in your terminal (same working directory) run “npm install express --save ”. It will install Express.js

Go to the package.json file below the “scripts” and add “start”: “node index.js”

{
“name”: “01webserver”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“start”: “node index.js”,
“test”: “echo \”Error: no test specified\” && exit 1"
},
“author”: “Rajesh”,
“license”: “ISC”,
“dependencies”: {
“express”: “⁴.17.1”
}
}

Write code in index.js file

const express = require('express')const app = express()app.get('/', function (req, res) {res.send('MY FIRST WEB SERVER')})app.listen(3000, () => {
console.log("server is running on port 3000")
})

Running your first server

On your terminal (same working directory) run the command “npm start ”.

Now you should see “server is running on port 3000” on your terminal.

Open your browser and go to “http://localhost:3000/” and you will see “MY FIRST WEB SERVER”

BINGO!!!

you have created your first web server.

Thank You so much for reading it till the end. I hope you enjoyed it and learn something valuable. If you want more such amazing content in the future then do follow me on Instagram and LinkedIn

--

--

Rajesh Bhattarai

I am a software developer who loves writing blogs about programming and web development.