The Wayback Machine - https://web.archive.org/web/20240908223055/https://www.geeksforgeeks.org/express-js-express-raw-function/
Open In App

Express.js express.raw() Function

Last Updated : 20 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The express.raw() function is a built-in middleware function in Express. It parses incoming request payloads into a Buffer and is based on body-parser. 

Syntax:

express.raw( [options] )

Parameter: The options parameter contains various properties like inflate, limit, type, etc. 

Return Value: It returns an Object. 

Installation of the express module:

You can visit the link to Install the express module. You can install this package by using this command.

npm install express

After installing the express module, you can check your express version in the command prompt using the command.

npm version express

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Example 1: Filename: index.js 

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
app.use(express.raw());
 
app.post('/', function (req, res) {
    console.log(req.body);
    res.end();
})
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Make sure you have installed the express module using the following command:

npm install express

Run the index.js file using the below command:

node index.js

Output:

Console Output:

Server listening on PORT 3000

Browser Output:

Now make a POST request to http://localhost:3000/ with header set to ‘content-type’ – ‘application/octet-stream’ and body { “name”:”GeeksforGeeks” }, then you will see the following output on your screen:

Example 2: Filename: index.js 

javascript




const express = require('express');
const app = express();
const PORT = 3000;
 
// Without this middleware
// app.use(express.raw());
app.post('/', function (req, res) {
    console.log(req.body);
    res.end();
})
 
app.listen(PORT, function (err) {
    if (err) console.log(err);
    console.log("Server listening on PORT", PORT);
});


Steps to run the program:

Run the index.js file using the below command:

node index.js

Now make a POST request to http://localhost:3000/ with header set to ‘content-type’ – ‘application/octet-stream’ and body { “name”:”GeeksforGeeks” }, then you will see the following output on your screen:

Output:

Server listening on PORT 3000
undefined

Reference: https://expressjs.com/en/4x/api.html#express.raw



Previous Article
Next Article

Similar Reads

How to access Raw Body of a Post Request in Express.js ?
Raw Body of a POST request refers to unprocessed or uninterpreted data sent in the request before Express or any middleware processes or understands it. It's like raw ingredients before the cooking begins. In this article we will see various approaches to access raw body of a post request in Express.js. Table of Content Using express.raw() Middlewa
3 min read
How to get raw content from a string including carriage return ?
The raw content of a string includes the carriage returns, i.e. it contains the new line escape sequence. Note: The line feed, represented by "\n" and a carriage return "\r" are very different. A line feed means moving the cursor one line forward. A carriage return means moving the cursor to the beginning of the line. Windows editors still use the
2 min read
How to Sign In with raw password when password store in the database are hashed form in Node.js ?
The password stored in the database is always in (hashed+salt) form because of security reasons. When users sign up on any website, a password is given in the raw form. But storing the raw passwords in the database directly is not a good practice. Using some server-side logic we first convert raw passwords in the (hashed+salt) form then store it in
5 min read
Node.js x509.raw Property
The x509.raw is an inbuilt application programming interface of class X509Certificate within the crypto module which is used to get a Buffer containing the DER encoding of this certificate. Syntax: const x509.raw Parameters: This property does not accept any arguments as a parameter. Return Value: This property returns a Buffer containing the DER e
2 min read
JavaScript String raw() Method
The raw() method in JavaScript is a template string tag function that returns the raw string form of template literals. It can be used to get the raw string form of a template literal without processing escape sequences. This method is useful when dealing with special characters that should not be escaped. Syntax: String.raw(callSite, ...substituti
2 min read
RAW to PNG Converter
It is a website where you can upload RAW pictures, and convert them into PNG files. You can then download the PNG version with just one click. We will also make sure the converted images have a standard size of 400×400 pixels. #content-frame {width: 700px; height: 500px;} @media (max-width: 780px) {.article--viewer .a-wrapper .content .text #conten
2 min read
TypeScript String.raw() Method
The String.raw() method in TypeScript is used to obtain the raw string representation of template literals without interpreting escape characters. This is particularly useful for preserving escape sequences and special characters. SyntaxString.raw(string: TemplateStringsArray, ...values: any[]): string String.raw`templateString`;Parametersstring (T
2 min read
Express.js express.text() Function
The express.text() function is a built-in middleware function in Express. It parses the incoming request payloads into a string and is based on body-parser. Syntax: express.text( [options] ) Parameter: The options parameter contains various properties like defaultCharset, inflate, limit, verify, etc. Return Value: It returns an Object. Installation
2 min read
Express.js express() function Complete Reference
Express.js is a small framework that works on top of Node.js 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. Express.js express() Methods:Method Description Express.js express.json() Function The express.json() function is a buil
1 min read
Express express.Router() Function
The express.Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.  Syntax: express.Router( [options] )Optional Parameters: case-sensitive: This enables case sensitivity.mergeParams: It preserves the req.params values from the parent router.stric
2 min read
Express express.urlencoded() Function
The express.urlencoded() function is a built-in middleware function in Express. It parses incoming requests with URL-encoded payloads and is based on a body parser. Syntax:  express.urlencoded( [options] )Parameter: The options parameter contains various properties like extended, inflate, limit, verify, etc. Return Value: It returns an Object. Step
2 min read
Express express.static() Function
The express.static() function is a built-in middleware function in Express. It serves static files and is based on serve-static.  Syntax: express.static(root, [options])Parameters: The root parameter describes the root directory from which to serve static assets.  Return Value: It returns an Object.  Steps to create the application:Step 1: Initiali
2 min read
Express JS express.json() Function
The express.json() function is a built-in middleware function in Express. It parses incoming requests with JSON payloads and is based on body-parser. It’s a critical component for handling JSON data in RESTful APIs and web applications, allowing the server to automatically parse JSON data from the request body. Syntax:express.json( [options] )Param
3 min read
Purpose of the express.Router middleware in Express
express.Router() in ExpressJS is like creating a mini-app within your main app. It helps organize routes and related functionalities, making it easier to manage different parts of your web application separately. express.Router() acts as a path organizer in ExpressJS, helping to structure and manage routes efficiently within a web application. Purp
1 min read
Explain the purpose of the express.static middleware in Express JS
The express.static middleware in ExpressJS simplifies serving static files like HTML, CSS, and images, improving performance and enhancing user experience in web applications. Purpose of the express.static middleware in ExpressJS:Serves Static Files: express.static middleware serves static files like HTML, CSS, JavaScript, and images from a specifi
2 min read
What is the purpose of the express-validator middleware in Express JS?
The express-validator middleware is a powerful tool for validating and sanitizing incoming request data in ExpressJS applications. It helps ensure that user input meets specified criteria, preventing security vulnerabilities and data corruption. With built-in error handling and customizable rules, express-validator seamlessly integrates with Expres
2 min read
How to Expire Session After 1 Min of Inactivity in express-session of Express.js ?
To automatically expire sessions after 1 minute of inactivity in an Express.js application using express-session, configure the session settings with the cookie.maxAge and implement session touch logic to track activity. ApproachTo expire the session after 1 min of inactivity in the express-session of Express.js we use expires: 60000 in the middlew
2 min read
What is express-session middleware in Express?
In the Express web application, the express-session middleware is mainly used for managing the sessions for the user-specific data. In this article, we will see the use of express-session middleware for session management in Express with practical implementation. PrerequisitesNode JSExpress JSTable of Content What is express-session Middleware?Step
2 min read
Express.js | app.disable() Function
The app.disable() function is used to set the boolean setting name to false. It is basically the shorthand for the app.set(name, false). So instead we can use app.disable(name) function to set the false boolean value to some system Express.js settings. Syntax: app.disable(name) Installation of the express module: You can visit the link to Install t
1 min read
Express.js | app.path() Function
The app.path() function returns the canonical path of the app as a string. When mounting advanced apps, the behavior of this approach can become extremely complicated. In most cases, it is better to use req.baseUrl to obtain the app's canonical path. Installation of the express module: You can visit the link to Install the express module. You can i
2 min read
Express.js | app.set() Function
The app.set() function is used to assign the setting name to value. You may store any value that you want, but certain names can be used to configure the behavior of the server. Syntax: app.set(name, value) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command. n
2 min read
Express.js | app.route() Function
The app.route() function returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors). Syntax: app.route( path ) Installation of the express module: You can visit the link to Install the express module. You can install this package
2 min read
Express.js | app.put() Function
The app.put() function routes the HTTP PUT requests to the specified path with the specified callback functions. Syntax: app.put(path, callback [, callback ...]) Arguments: Path: The path for which the middleware function is invoked and can be any of the:A string represents a path.A path pattern.A regular expression pattern to match paths.An array
2 min read
Express.js | app.METHOD() Function
The app.METHOD() function is used to route an HTTP request, where METHOD is the HTTP method of the request, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are app.get(), app.post(), app.put(), and so on. Syntax: app.METHOD(path, callback [, callback ...]) Parameters: Path: The path for which the middleware function is inv
2 min read
Express.js | app.param() Function
The app.param() function is used to add the callback triggers to route parameters. It is commonly used to check for the existence of the data requested related to the route parameter. Syntax: app.param([name], callback) Parameters: name: It is the name of the parameter or an array of them.callback: It is a function that is passed as a parameter. In
2 min read
Express.js res.location() Function
The res.location() function is used to set the response Location HTTP header to the path parameter which is being specified. Basically, it is used to set the response header. It doesn't end the response, after using it you can write a response body if you want to write. Syntax: res.location( path ) Parameter: path: It refers to the URL which is spe
2 min read
Express.js | app.all() Function
The app.all() function is used to route all types of HTTP requests. Like if we have POST, GET, PUT, DELETE, etc, requests made to any specific route, let's say /user, so instead of defining different APIs like app.post('/user'), app.get('/user'), etc, we can define single API app.all('/user') which will accept all type of HTTP request. Syntax: app.
2 min read
Express.js | app.render() Function
The app.render() function is used to render the HTML of a view via the callback function. This function returns the HTML in the callback function. Syntax: app.render(view, [locals], callback) Parameters: view: It is the name of the HTML page which is to be rendered.locals: It is an optional parameter that is an object containing local variables for
2 min read
Express.js | app.get() Function
The app.get() function returns the value name app setting. The app.set() function is used to assign the setting name to value. This function is used to get the values that are assigned. Syntax: app.get(name) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command.
1 min read
Express.js | app.enabled() Function
The app.enabled() function is used to return the bool values of the setting name. It returns true if the setting name is enabled and returns false if the setting name is not enabled. Syntax: app.enabled(name) Installation of the express module: You can visit the link to Install the express module. You can install this package by using this command.
1 min read