9951 explained code solutions for 126 technologies


expressjsHow do I use Express.js to process form data?


Express.js is a web application framework for Node.js designed to make building web applications easier. It can be used to process form data in the following way:

  1. Create a form in HTML and add an action attribute to the form element which points to the route that will handle the form submission.
<form action="/form-submission" method="POST">
  <input type="text" name="name" />
  <input type="submit" value="Submit" />
</form>
  1. Create a route to handle the form submission.
app.post('/form-submission', (req, res) => {
  const name = req.body.name;
  res.send(`Hello ${name}`);
});
  1. When the form is submitted, the req.body object will contain the data from the form.

Output example

Hello <name>

  1. Finally, you can use the data from the form in your application.

Helpful links

Edit this code on GitHub