Categories
JavaScript Answers

How to accept form data with Express.js?

Spread the love

Sometimes, we want to accept form data with Express.js.

In this article, we’ll look at how to accept form data with Express.js.

How to accept form data with Express.js?

To accept form data with Express.js, we can use the express-formidable package.

To install it, we run

npm install express-formidable

Then we use it by writing

const express = require('express');
const formidable = require('express-formidable');

const app = express();

app.use(formidable());

app.post('/upload', (req, res) => {
  res.send(JSON.stringify(req.fields));
});

to call formiable to return a middleware that parses form data request data.

Then we call app.use with the returned middleware to use it.

Next, in the route handler callback, we get the form data request data from req.fields.

Conclusion

To accept form data with Express.js, we can use the express-formidable package.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *