Unique features of Express JS - GeeksforGeeks (2024)

Last Updated : 23 Nov, 2023

Summarize

Comments

Improve

Express.js is known for its simplicity and flexibility which comes with various features that helps you to build efficient and scalable web applications. It makes it easier to organize your application’s functionality with middleware and routing. It adds helpful utilities to Node.js HTTP objects and facilitates the rendering of dynamic HTTP objects.

Let’s dive into some of the unique features of Express.js along with its examples.

Table of Content

  • Routing
  • Middlewares
  • Error Handling
  • Body Parsing

1. Routing

Routing is the process of handling an HTTP request that defines which kind of response will be sent to the client on which particular request. In Node JS, we have a module called http to create a server, where we create a server using http.createServer and pass a callback function to http.createServer, where we get requests and responses as s parameter and using if else and URL, we setup routes.

Node JS:

if (method === 'GET' && url === '/') {
res.end('Hello, World!');
}

While in Express JS, routing and creating servers is an inbuilt feature, we don’t need to setup if else statements to setup routes. We can directly use the simple methods of Express JS to setup routes.

Express JS:

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

2. Middlewares

Middlewares are the middle processes that execute between processes. In terms of web development, when we store passwords in a database using a server, we use middleware to encrypt our passwords to make them secure. But Node JS does not contain any middleware by default, but we can create our own custom middleware in it. Instead of Node JS, Express.js contains built-in middleware like express.static() to server static files to clients.

Syntax:

app.use(express.static('public')); 

3. Error Handling

Error handling is used to ensure the smooth running of your software or program in case of any undetected issue in your software. But in Node JS, there is no way to handle errors automatically through any module. Developers can setup error handling using try catch blocks or event emitters. But in Express JS, it is much easier to handle errors because there are multiple ways to handle errors, like asynchronous handling, global error handling, etc.

Syntax:

 throw new Error('Error');

4. Request & Response Object

Request and Response means what is requested by the client side and, in exchange for that request, what data is sent to the client side from the server side in terms of response. The request and response object is contained in both Node JS and Express JS, but still, Express JS comes with multiple additional functionalities in this object. For example, Express JS allows developers to use parameters to access URLs, and res.send() is a much more convenient way to send responses. It also allows user middlewares to be used in server-side coding.

Syntax:

app.get('/', (req, res) => {
console.log(req.method);
console.log(req.url);
res.send('Hello, World!');
});

5. Body Parsing

Body parsing refers to parsing data sent from the client side to the server side. The client sent data in the body of the request and also sent the type of content in headers, so converting data according to the content type is called body parsing. In Node.js, there is no built-in method or function to parse client-side data, but we can use modules like querystring or buffer. But Express JS contains built-in modules to parse data without any external modules like middleware or Express. json() Parsing.

Syntax:

app.use(express.json()); 

Example: Implementation of above features.

Javascript

const express = require('express');

const app = express();

const port = 3000;

app.use(express.json());

app.use((req, res, next) => {

console.log(`MiddleWare Accepted`);

next();

});

app.get('/', (req, res) => {

res.send('Hello, World!');

});

app.post('/test', (req, res) => {

console.log(req.body);

res.send("Data Recieved!")

});

app.use((err, req, res, next) => {

console.error(err.stack);

res.status(500).send('Something went wrong!');

});

app.listen(port, () => {

console.log(`Server Established on Port -> ${port}`);

});

Output:

Unique features of Express JS - GeeksforGeeks (1)



Unique features of Express JS - GeeksforGeeks (2)

Improve

Please Login to comment...

Unique features of Express JS - GeeksforGeeks (2024)
Top Articles
Finance 101: Investment Types: Bonds, Stocks, Mutual Funds - City Girl Savings
How to Increase a Credit Score to 800 – 5 Proven Tips! | Middle Class Dad Money
Victory Road Radical Red
Gamevault Agent
South Carolina defeats Caitlin Clark and Iowa to win national championship and complete perfect season
O'reilly's In Monroe Georgia
Overzicht reviews voor 2Cheap.nl
Infinite Campus Parent Portal Hall County
4302024447
Busty Bruce Lee
The fabulous trio of the Miller sisters
State HOF Adds 25 More Players
Check From Po Box 1111 Charlotte Nc 28201
Xomissmandi
Tvtv.us Duluth Mn
Spoilers: Impact 1000 Taping Results For 9/14/2023 - PWMania - Wrestling News
Costco Great Oaks Gas Price
Drago Funeral Home & Cremation Services Obituaries
Marion City Wide Garage Sale 2023
About My Father Showtimes Near Copper Creek 9
The Listings Project New York
Rs3 Ushabti
Dtm Urban Dictionary
Inter Miami Vs Fc Dallas Total Sportek
Publix Near 12401 International Drive
Yale College Confidential 2027
Democrat And Chronicle Obituaries For This Week
TMO GRC Fortworth TX | T-Mobile Community
Tactical Masters Price Guide
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
Supermarkt Amsterdam - Openingstijden, Folder met alle Aanbiedingen
Peter Vigilante Biography, Net Worth, Age, Height, Family, Girlfriend
Www Violationinfo Com Login New Orleans
Junior / medior handhaver openbare ruimte (BOA) - Gemeente Leiden
Closest 24 Hour Walmart
The 50 Best Albums of 2023
Game8 Silver Wolf
Elisabeth Shue breaks silence about her top-secret 'Cobra Kai' appearance
Pepsi Collaboration
Mytime Maple Grove Hospital
QVC hosts Carolyn Gracie, Dan Hughes among 400 laid off by network's parent company
Wisconsin Volleyball titt*es
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Www.homedepot .Com
Dietary Extras Given Crossword Clue
Lightfoot 247
Skyward Login Wylie Isd
Round Yellow Adderall
8663831604
When Is The First Cold Front In Florida 2022
Southern Blotting: Principle, Steps, Applications | Microbe Online
Mast Greenhouse Windsor Mo
Latest Posts
Article information

Author: The Hon. Margery Christiansen

Last Updated:

Views: 6044

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: The Hon. Margery Christiansen

Birthday: 2000-07-07

Address: 5050 Breitenberg Knoll, New Robert, MI 45409

Phone: +2556892639372

Job: Investor Mining Engineer

Hobby: Sketching, Cosplaying, Glassblowing, Genealogy, Crocheting, Archery, Skateboarding

Introduction: My name is The Hon. Margery Christiansen, I am a bright, adorable, precious, inexpensive, gorgeous, comfortable, happy person who loves writing and wants to share my knowledge and understanding with you.