ExpressJS Web Application Framework | Node.Js

What is Node.Js?

Node.js is an open-source and cross-platform Javascript runtime environment.
It is a way for us to write javascript on the server-side.Node.js provides asynchronous execution that is handling the events without interrupting the main thread. Node is based on a Non-blocking model which makes it faster for building any web application. It is used for building real-time applications.

For writing Node.js on the server-side we need to include libraries, npm is a way to get those libraries.
Npm(Node Package Manager) provides packages for Node.js.You can download thousands of free packages using npm.
You don’t need to install or download npm it gets automatically install when you download node.js.

What is Express?

Express is a web-development light-weight framework for Node.js.It is very flexible. It is one of the most downloaded npm packages.
Express manages everything from router to handling requests. It also makes it easy to work with routing patterns, cookies, and sessions.

Installing Express.js

Below is how we can install the Express.js through npm:

npm install express --save

The above command will install express which you can see in the node_modules folder which will be in the root directory of your project.

First Express App

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

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

const port=5000;

app.listen(port,()=>{
   console.log(`Server started on port ${port}`);
});

The first line of the code will require the express module. It’s basically an import statement.
The const app=express() will have all HTTP requests, middleware, etc.

app.get('/',(req,res)=>{
   console.log("Hello World");
});

app. get() is a route that will be invoked whenever there is a GET request with a path(‘/’).

app.listen(port,()=>{ 
    console.log(`Server started on port ${port}`); 
});

The above line of code will start the server on port 5000.

Save the code in a file named app.js and run it with the following command in your terminal.

node app.js

 

Output:

To see the output you can use any browser(Chrome, Firefox, or whatever browser you’re comfortable with)

You can access the web app via URL  http://localhost:5000.

You can now see the text “Hello World” on your web page.

Hello World Web Page

 

Leave a Reply

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