In web applications, handling user input from forms is a common task. Express.js provides easy ways to capture and process this data from GET or POST requests.
Express uses middleware to parse incoming request data:
JSON Data – Sent by APIs or JavaScript frontends.
GET requests send data via query parameters:
HTML Form Example:
Express Route:
URL: /search?q=express → Response: You searched for: express
POST requests send data via the request body:
HTML Form Example:
Express Route:
For APIs or JavaScript fetch requests:
Express Route:
Fetch Example:
Route parameters can also be used to capture data from the URL:
URL: /users/123 → Response: User ID: 123
Use express.json() for APIs and express.urlencoded() for HTML forms.
Always validate and sanitize user input before using it.
Use POST for sensitive data (e.g., passwords) and GET for simple queries.
Modularize routes for cleaner code in large projects.
views/form.ejs:
app.js:
Handling forms and request data is essential for building interactive web applications, whether for APIs or server-rendered HTML forms.
URL-encoded Data – Sent by HTML forms.
Take quizzes related to this topic and see where you stand!
Start Quiz Now