Knowee
Questions
Features
Study Tools

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.

Question

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.

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

Solution

Sure, here is a step-by-step guide on how to build an API endpoint in Node.js using Express.js:

  1. First, you need to install Node.js and Express.js. If you haven't installed Node.js, you can download it from the official website. Once you have Node.js installed, you can install Express.js by running the following command in your terminal:
npm install express
  1. Create a new file, let's call it app.js, and open it in your text editor.

  2. Import the express module and initialize an express application. Add the following code to app.js:

const express = require('express');
const app = express();
  1. Now, let's create the API endpoint. We want it to be a GET request at the path '/name'. We also want to respond with a JSON document with the structure { name: 'firstname lastname' }. The first and last name parameters should be encoded in a query string. Here is how you can do it:
app.get('/name', (req, res) => {
  const firstname = req.query.first;
  const lastname = req.query.last;
  const name = `${firstname} ${lastname}`;

  res.json({ name: name });
});
  1. Finally, we need to start the server. We can do this by calling the listen method on our app. Let's have our server listen on port 3000:
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
  1. Save your app.js file. You can start your server by running the following command in your terminal:
node app.js
  1. Now, if you navigate to http://localhost:3000/name?first=firstname&last=lastname in your web browser, you should see a JSON document with the structure { name: 'firstname lastname' }.

Note: In the next exercise, you will receive data from a POST request at the same '/name' route path. You can use the method app.route(path).get(handler).post(handler) to chain different verb handlers on the same path route. This will allow you to save a bit of typing and have cleaner code.

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.

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.

Generate User NameFix getUserName so that it returns the user’s name as "LastName, FirstName" (without the quotes).Capitalise the first letter of the first and last namesIf the lastname is null, just return firstname without a commae.g. "John" "Smith" should return "Smith, John"

What is a query string in a Web API request?

The attribute name could be structured as an attribute consisting of first name, middle initial, and last name. This type of attribute is calledMarks : 1Negative Marks : 0Answer hereSimple attributeComposite attributeMultivalued attributeDerived attribute

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.