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!' ); }); |
Why Express is used in Web Development?
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.
Table of Content
- What is Express JS?
- Why Express is used in web development?
- Steps to create an Express application