2.6 KiB
2.6 KiB
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7fb1367417b2b2512bf1 | Serve JSON on a Specific Route | 2 | 301517 |
Description
/json
. You can do it as usual, with the app.get()
method. Inside the route handler, use the method res.json()
, passing in an object as an argument. This method closes the request-response loop, returning the data. Behind the scenes, it converts a valid JavaScript object into a string, then sets the appropriate headers to tell your browser that you are serving JSON, and sends the data back. A valid object has the usual structure {key: data}
. data
can be a number, a string, a nested object or an array. data
can also be a variable or the result of a function call, in which case it will be evaluated before being converted into a string.
Instructions
{"message": "Hello json"}
as a response, in JSON format, to GET requests to the /json
route. Then point your browser to your-app-url/json
, you should see the message on the screen.
Tests
tests:
- text: 'The endpoint <code>/json</code> should serve the json object <code>{"message": "Hello json"}</code>'
testString: 'getUserInput => $.get(getUserInput(''url'') + ''/json'').then(data => { assert.equal(data.message, ''Hello json'', ''The \''/json\'' endpoint does not serve the right data''); }, xhr => { throw new Error(xhr.responseText); })'
Challenge Seed
Solution
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/