* Expanded the solution for the 'Get Route Parameter Input from the Client' challenge * Expanded the guide for the 'Use body-parser to Parse POST Requests' challenge * Rewritten guide for the 'Serve JSON on a Specific Route' challenge and fixed source link * Expanded the guide for the 'Serve Static Assets' challenge * Expanded solution to the 'Get Query Parameter Input from the Client' challenge and fixed links to source file * Added solution to the 'Chain Middleware to Create a Time Server' challenge and fixed link to source file * Rewrite the 'Start a Working Express Server' challenge * Expanded the guide for 'Expand Your Project with External Packages from npm' * Added reference to semantic versioning in 'Add a Version to Your package.json' * fix/remove-links+fix-solutions * fix/remove-more-links
36 lines
966 B
Markdown
36 lines
966 B
Markdown
---
|
|
title: Get Route Parameter Input from the Client
|
|
---
|
|
## Get Route Parameter Input from the Client
|
|
|
|
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
|
|
|
|
If someone tells you to build a GET or POST endpoint you would achieve the same using `app.get(...)` or `app.post(...)` accordingly.
|
|
|
|
## Hint
|
|
|
|
In order to get route parameters from a POST request, the general format is as follows:
|
|
|
|
```javascript
|
|
app.post("/:param1/:param2", (req, res) => {
|
|
// Access the corresponding key in the req.params
|
|
// object as defined in the endpoint
|
|
var param1 = req.params.parameter1;
|
|
// OR use destructuring to get multiple paramters
|
|
var { param1, param2 } = req.params;
|
|
// Send the req.params object as a JSON Response
|
|
res.json(req.params);
|
|
});
|
|
```
|
|
|
|
## Solution
|
|
|
|
```javascript
|
|
app.get("/:word/echo", (req, res) => {
|
|
const { word } = req.params;
|
|
res.json({
|
|
echo: word
|
|
});
|
|
});
|
|
```
|