Create your first app in Node.js with Express.js

From this tutorial, I have started this tutorial series on Node.js, It will be from the basic to expert level and I assure that it will be better than video tutorial. I am working on Node.JS since 3+ years now and still upgrading my self by learning and working day by day. I have choose node.js because, language used to right code in nodejs is JavaScript and good thing is I was working on JavaScript since 6 years. Therefor I decided that I will work on this technology and grow my self in my web development career.

Create First app in Node.js

Before starting this series I am assuming that you have installed Node.JS in your system or you can easily go through the NodeJS official site and complete the installation process. After the Node.JS installation you should install NPM package manager it will require in every project. I will discuss about the NPM in details later on.

Install NPM using cli

People can skip this who already have installed and who don’t can continue. Open your command prompt if you are using windows or if you are using Linux you can open terminal and type below command.

npm install [email protected] -g

This command can install or upgrade to latest version.

Pre-request

  1. Node.JS installed
  2. NPM installed
  3. Basic JavaScript or programming knowledge in any language

If you would like to learn JavaScript from basic you can follow my JavaScript Category – Learn JavaScript from Basic.

Let’s get back to the topic and start. What you will learn at the end of this tutorial. You will learn about http module, what is express, how to create server in nodejs using http module and print “Hello World”.

First of all we will create the project, so let create the project directory first and get into the project folder using command prompt.

>mkdir myfirstapp
>cd myfirstapp

Now initialize the project using below command. It will generate a package.json file which contains the details of the project dependencies and modules:

>npm init -y

Next task is to install express.js module into the project and also create javascript file name app.js and server.js

>npm i express  // It will install express.js module
>touch app.js server.js // It will generate two .js files

Lets discuss precisely about Express.js. Express.js is a web app framework for node.js. It provide many feature which makes development easier and organized. I will discuss more on Express.js on upcoming tutorials. Now you should have following file structure if you followed properly.

First app in nodejs

Import Express.js and write routes as a module

Now, start coding, I will write fist on on app.js you will understand below why I am writing first in app.js.

  • In app.js I will import express.js module first
  • Then I will assign express() method to app variable.
  • In third line I will create route one for get and one for post.
  • Then export app module to make available for server.js

We will create server using http module in server.js. Obviously we can create server using express but I believe that using http module will start faster because it have less dependencies.

const express = require('express'); // import express
const app = express();  // create app method

// this is a home route can be accessed from the browser
app.get('/',function(req, res){

  res.send("Hello World!");

});

// Post method can be accessed from api request
app.post('/',function(req, res){

    res.send("Hello World!");
  
});

module.exports = app; // export this module 

Now start writing code in server.js follow the below code to create server, import app and listen server on port 3000. In server.js first of all inport the http module then import app.js module which I have coded above. then create server using http module and then listen the server on port 3000. We can use any free port.

const http = require("http");  // Import http module 
const PORT = 3000; // We can any free port
const app = require('./app');  // Import app.js module

const server = http.createServer(app); // Create http server

// Now listen server on port
server.listen(PORT, ()=>{

  console.log("Server is running on http://localhost:"+PORT);

});

Now the coding part is done, now lets run the server from by using command below:

>node server.js

Output on the browser you should get

Hello World in NodeJS

API end point access via Postman

To test the post route we will use Postman. Postman is an application using it we can simulate the api calls. Postman is very useful for api testing purpose. There are many application like postman but I am using postman since 3 years it runs very fast and smooth.

Hello World in NodeJS

Now wrap up today’s tutorial:

We have learned how to create app in node.js using express.js and also learned how create, import and export modules in node.js. So, I think everything is discussed what we aimed initially. If you have any kind of query regarding please ask in comment. I will try to solve on my best way.

Leave a Reply

Your email address will not be published. Required fields are marked *