Visualize and understand how data sent from the client is processed and represented in the `req` object on the server side in an Express.js application.
### HTML and JavaScript to Present a Route and Print the req Object Contents
The following example demonstrates how to create a route that prints out the contents of the `req` object in an Express.js application. The HTML and JavaScript will allow you to submit data to this route and display the server's response.
- **Backend (server.js)**:
- The server has a route `/print-req` that responds with the contents of the `req` object in JSON format.
- The middleware is set up to parse URL-encoded bodies and JSON.
- **Frontend (index.html, style.css, script.js)**:
- `index.html` contains a form for the user to submit their name and email.
- `style.css` provides basic styling to make the form look presentable.
- `script.js` handles the form submission using the Fetch API to send the form data as a JSON object to the server and then displays the server's response (which includes the `req` object) in a `<pre>` tag.
By using this setup, students can visualize and understand how data sent from the client is processed and represented in the `req` object on the server side in an Express.js application.
Final version:
To properly test the application, you should follow these steps:
Navigate to the Root URL:
Open your browser and go to http://localhost:3000.
This should display the form where you can enter your name and email.
Fill Out and Submit the Form:
Enter your name and email in the form fields.
Click the "Submit" button to send the form data to the /print-req route using a POST request.
Here is a summary of what you need to do:
Access the Form
Open your browser and go to http://localhost:3000.
You should see the form as described in the previous instructions.
Fill Out and Submit the Form
Enter your name and email in the form fields.
Click the "Submit" button.
Review the Response
The JavaScript on the client side will handle the form submission.
It will send the form data as a JSON object to the /print-req route using a POST request.
The server will respond with the contents of the req object, which will be displayed in the <pre> element on the page.
Example: Correct Workflow
Go to: http://localhost:3000 in your web browser.
Fill in the form with your name and email.
Submit the form by clicking the "Submit" button.
Here is an additional check to ensure everything is set up correctly:
Ensure All Files Are Correct
server.js: Back End File
const express = require('express');
const app = express();
const port = 3000;
// Middleware to parse URL-encoded bodies and JSON
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve static files
app.use(express.static('public'));
// Route to print out the req object
app.post('/print-req', (req, res) => {
res.json(req); // Responds with the entire req object as JSON
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);