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
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
- Request & Response Object
- Body Parsing