Node and Express - Handling HTML Forms
Learn everything about Express in this Node and Express tutorial series. This article goes in depth on how to handle HTML forms with Express. We will also learn about the express.json and express.urlencoded middleware.
Table of Contents 📖
- What is an HTML Form?
- What is an HTTP POST Request?
- How to Create an HTML Form?
- Creating an HTML Form with Pug
- Form Action Attribute
- Form Method Attribute
- Styling our Pug Form
- Creating a Route to Display our Pug Form
- Registering our Express Router with our Express App
- Displaying our Pug Form
- Configuring Express to Handle Form Data
- Why do we need these Middleware Functions?
- What is express.json()?
- What is the Content-Type Header?
- What is express.urlencoded({ extended: true })?
- Creating a POST Route
- What is request.body?
- Name Attribute
- Summary
What is an HTML Form?
An HTML form is a group of one or more fields on a webpage used to collect information. It is typically submitted to a server as a POST request for processing. A common HTML form is a login form containing two inputs for username and password and a button for submitting the username and password to the server.
What is an HTTP POST Request?
An HTTP POST request is used to send data to a server, often to create a resource. The data that is sent to the server is stored in the body of the HTTP request. Below is an example of an HTTP POST request that sends json data to the login route on wittcode.com.
POST /login HTTP/1.1
Host: wittcode.com
Content-Type: application/json
Content-Length: 30
{"username": "WittCode", "password": "soccer"}
How to Create an HTML Form?
An HTML form is defined inside