expressjsHow do I use Express.js to handle x-www-form-urlencoded data?
Express.js is a web application framework for Node.js. It can be used to handle x-www-form-urlencoded data. To do this, you need to install the body-parser middleware. This middleware is used to parse incoming request bodies. Here is an example of how to use body-parser to handle x-www-form-urlencoded data:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/form', (req, res) => {
console.log(req.body);
res.send('Form Received');
});
This example creates an Express server that listens for POST requests to the /form
endpoint. The body-parser middleware is used to parse the x-www-form-urlencoded data in the request body. The parsed data is then available in the req.body
object.
Code explanation
const express = require('express');
- This imports the Express module.const bodyParser = require('body-parser');
- This imports the body-parser middleware.const app = express();
- This creates an Express application.app.use(bodyParser.urlencoded({ extended: false }));
- This configures the body-parser middleware to parse x-www-form-urlencoded data.app.post('/form', (req, res) => {
- This defines a route handler for POST requests to the/form
endpoint.console.log(req.body);
- This logs the parsed x-www-form-urlencoded data from the request body.res.send('Form Received');
- This sends a response to the client.
Helpful links
More of Expressjs
- How can I use express-zip js to zip and download files?
- How do I find Express.js tutorials on YouTube?
- How do I use Express.js to create a cluster?
- How can I use Express.js and Vite together for software development?
- How do I set the time zone in Express.js?
- How do I use adm-zip with Express.js?
- How do I set up a YAML configuration file for a Node.js Express application?
- How do I download a zip file using Express.js?
- How can I use Zipkin to trace requests in Express.js?
- How do I use Zod with Express.js?
See more codes...