Open In App

Why Express is used in Web Development?

Last Updated : 16 Dec, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Express JS is a small web application framework. It become very popular Node JS framework for building robust and scalable application. In this article we will dive into the topic of ExpressJ S.

What is Express JS?

Express is a small framework that sits on top of NodeJS web server functionality to simplify its APIs and add helpful new features. It makes it easier to organize your application’s functionality with middleware and routing. It adds helpful utilities to NodeJS HTTP objects, it helps the rendering of dynamic HTTP objects. Express is a part of MEAN stack, a full-stack JavaScript solution used in building fast, robust, and maintainable production web applications.

Why Express is used in web development?

Express JS become the popular choice in web development because it simplifies the process of building web applications using Node JS. There are many features provided by express.js. Some common features are provided by the express.js.

1. Routing: Express provides a routing facility. The developer can set routes using express with different HTTP verbs(GET, POST, PUT, etc). It can be associated with different functions. It handles different requests.

Example: The following program demonstrates "How we can define the Routing in Express.js"

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

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


2. Middleware: The middleware functions are a core concept of node js. These functions can access the 'req' and 'res' response object. They can modify these object before reaching the route handler. The following program demonstrates the Middleware function.

JavaScript
app.use((req, res, next) => {
    console.log('Middleware executed!');
    next(); // Pass control to the next middleware or route handler
});

3. Error Handling: Express js can handle errors very easily. It can handle both type of requests like synchronous and asynchronous requests. The middleware function can used to catch the errors. The following program demonstrates the Error Handling.

JavaScript
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something went wrong!');
});

Steps to create an Express application

Step 1: Creating a directory for our project and make that our working directory.

mkdir gfg
cd gfg


Step 2: Using npm init command to create a package.json file for our project.

npm init


Step 3: Install ExpressJS with the following command.

npm i express


Example: The following program demonstrates the how we can use express.js

JavaScript
//app.js

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

// Define a route that handles GET requests to the root URL
app.get('/', (req, res) => {
    // Send a response to the client
    res.send('Hello, Express!');
});

// Start the server and listen on port 3000
app.listen(3000, () => {
    console.log('Server is listening on port 3000');
});

Run the following command to run the application

node app.js


Output:

Image



Next Article

Similar Reads

three90RightbarBannerImg
  翻译: