Knowee
Questions
Features
Study Tools

Mount a POST handler at the path /name. It’s the same path as before. We have prepared a form in the html frontpage. It will submit the same data of exercise 10 (Query string). If the body-parser is configured correctly, you should find the parameters in the object req.body. Have a look at the usual library example:route: POST '/library'urlencoded_body: userId=546&bookId=6754req.body: {userId: '546', bookId: '6754'}Respond with the same JSON object as before: {name: 'firstname lastname'}. Test if your endpoint works using the html form we provided in the app frontpage.

Question

Mount a POST handler at the path /name. It’s the same path as before. We have prepared a form in the html frontpage. It will submit the same data of exercise 10 (Query string). If the body-parser is configured correctly, you should find the parameters in the object req.body. Have a look at the usual library example:route: POST '/library'urlencoded_body: userId=546&bookId=6754req.body: {userId: '546', bookId: '6754'}Respond with the same JSON object as before: {name: 'firstname lastname'}. Test if your endpoint works using the html form we provided in the app frontpage.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To handle a POST request at the path /name, you would need to use Express.js, a web application framework for Node.js. Here's a step-by-step guide on how to do it:

  1. First, you need to install Express.js and body-parser. You can do this by running the following commands in your terminal:
npm install express --save
npm install body-parser --save
  1. Next, you need to set up your server. Here's a basic setup:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
  1. Now, you can mount a POST handler at the path /name. Here's how you can do it:
app.post('/name', (req, res) => {
  const { firstname, lastname } = req.body;
  res.json({ name: `${firstname} ${lastname}` });
});

In this code, app.post('/name', (req, res) => {...}); is used to handle POST requests at the path /name. The callback function takes two arguments: req (the request object) and res (the response object).

The line const { firstname, lastname } = req.body; extracts the firstname and lastname from the request body.

The line res.json({ name: firstname{firstname} {lastname} }); sends a JSON response with the name.

  1. Finally, you can test your endpoint by submitting the form on the app frontpage. If everything is set up correctly, you should receive a JSON response with the name.

This problem has been solved

Similar Questions

Build an API endpoint, mounted at GET /name. Respond with a JSON document, taking the structure { name: 'firstname lastname'}. The first and last name parameters should be encoded in a query string e.g. ?first=firstname&last=lastname.Note: In the following exercise you are going to receive data from a POST request, at the same /name route path. If you want, you can use the method app.route(path).get(handler).post(handler). This syntax allows you to chain different verb handlers on the same path route. You can save a bit of typing, and have cleaner code.

body-parser has already been installed and is in your project's package.json file. require it at the top of the myApp.js file and store it in a variable named bodyParser. The middleware to handle URL encoded data is returned by bodyParser.urlencoded({extended: false}). Pass the function returned by the previous method call to app.use(). As usual, the middleware must be mounted before all the routes that depend on it.

In the following exercise you are going to receive data from a POST request, at the same /name route path. If you want, you can use the method app.route(path).get(handler).post(handler). This syntax allows you to chain different verb handlers on the same path route. You can save a bit of typing, and have cleaner code.

What request methods does this line of code below [email protected]('/test')

Which middleware is commonly used to parse incoming request bodies in Express?express.urlencoded()express.bodyParser()body-parserbodyHandler

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.