Handling Form Submission
Explore how to handle form submissions in an Express web server using TypeScript. Learn to create and render form templates, configure body-parser middleware, and process POST request data from user input for login functionality.
Let’s now take a look at how Express can interpret data that is submitted from a
form on a web page. To do this, we will create a login.hbs template file and modify our login.ts route handler to accept a POST request, along with the existing GET request.
Creating the login template
We can create a file in the views directory named login.hbs as follows:
Here, we have an HTML template that contains a form.
-
Line 3: Within this form, we have a paragraph element that will show the
errorMessageproperty if it exists. -
Lines 4–9: We then have an input control with the name attribute set to
username, and another input control ...