How to start a project with node.js and Express.js — For beginners

What is Node.js ?

What is Express.js?

Node.js is a javascript runtime environment that has capabilities for server-side developments. It’s built on Chrome’s V8 engine. Node is asynchronous. That means it doesn’t block itself just for one request. It can handle multiple requests. As soon as Node starts it initiates all the variables and functions and waits for an event to occur.

Express.js is a node.js framework that provides awesome features to build web and mobile applications. We can use Express to build single-page multi pages or hybrid web applications. It has built on top of the node.js that helps to manage servers and routes.

Here, I’m listing down what are the major areas that I’m gonna cover in this article. First of all, you need to know some of the basic tasks that are very important when it comes to building a web application.

How to install a code editor.

How to install Node.js on the computer.

How to install an NPM package.

Install Visual cade Edior

There are a bunch of code editors available for programmers. But VS code is the more popular one since it is free, light-weight, less complex, cool extensions, etc. So, my recommendation is to get VS code as your code editor. Download it here.

Install Node on your computer

Instaling Node on your computer is a piece of cake. You just need to visit the Node side and download the recommended version for most users. Then all you need is to click that downloaded .exe file and follow the simple installation process. You can check whether it is successful or not by typing node — version or node -v in the command prompt.

Install NPM packages on the project host thousand of NPM packages for various objectives. There is a simple command for installing NPM packages. To install a any NPM package you need to type the below command in the cmd:

npm install package_name

There is a short version also available.

npm i package_name

To uninstall a package you can use this:

npm uninstall package_name

Next, I’m gonna tell you how you can build a node.js application step by step.

Execute your first program on Node.

How to import a Node module or NPM package.

How to initiate NPM.

How to install Express and create the express application.

How to start the server on Port 5000.

How to define routes with different methods.

Let’s jump into the most important topics in this article. You’ll be able to learn all the basics that need to know when developing a node application.

Execute your first node application

knowing basic knowledge of javascript is enough to implement this. Open the VS code and create a file index.js in your project directory. You can use whatever name you want. Then inside that file type :

console.log("Hello Node, This is my first program");

Then, open the terminal in VS code and navigate to the file path to type node index.js or node index . You can see the output in the terminal.

Import a node module or NPM package

Node.js has a set of inbuild modules that can be used directly without any installation. Look here to refer to more detail about node inbuild modules. Let’s start with a simple one, that is fs(file system). We can use this to write, create and modify files. You can import and assign it to a variable like this:

const fs = require('fs');

You can import an NPM package also like this. So, importing a node module or NPm package is very clear.

const suitable_variable_name = require('Node_Module_name');const suitable_variable_name = require('NPM_package_name');

Initiate NPM in your program

This is also a simple process. You just need to run the below command in the terminal to initiate NPM. Then some questions will appear. Answer them accordingly or press Enter for all. package.json file will be created after executing this.

npm init

If you press Enter for all questions, you have a shortcut to do this. you can type npm init -y instead of npm init .

Install Express and create the express application

you can install express into your application just like installing an NPM package(explained before).

npm install express

Then, you can import and create the express application like this:

const express = require('express');const app = express();

Start the server on port 5000

Bind and listen to the connections on the specified host and port you can use app.listen() function.

const PORT = 5000;app.listen(PORT, () => {   console.log(`Server is running on port ${PORT}`);});

This is the application that we have built so far.

Create some routes with different HTTP methods

The most commonly-used HTTP methods are POST, GET, PUT, PATCH, and DELETE. You can create a route with theGET method asapp.get(route, callback function) . The call back function has two parameters as req and res. It is not compulsory to use it as req and res . You can use any name.

app.get("/", (req, res) => {  res.send("Hello, This is start of the application");});

You can create any route like this. I have taught you how to start a node application with express. This is the end of this article. But I’m gonna tell you one more interesting fact. That is nodemon NPM package. This package is so useful when you build an application. You have to run the server every time when you do a change to the application. nodemon solve this problem easily. If you use nodemon you don’t have to run the server every time. Just one time is enough.

npm install nodemon

After installing you have to add this "start" : "nodemon index.js"script to the package.json file. Then run the server again.

See you in the next blog post. Bye Bye🍻🍸❤️❤️



Undergraduate of University of Moratuwa | Faculty of Information Technology

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
Kushan Madhusanka

Undergraduate of University of Moratuwa | Faculty of Information Technology