Created
November 8, 2020 18:16
-
-
Save KenanBek/daa4fe9d9ca273cf7afab9c602c20252 to your computer and use it in GitHub Desktop.
Use res.json() and accept URL parameters by using req.params object. More: https://kananrahimov.com/post/example-backend-api-in-node-js-video-tutorial/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
//const hostname = '127.0.0.1'; | |
const port = 3000; | |
app.get('/', (req, res) => { | |
let body = { | |
"message": "Hello, world!", | |
"items": [ | |
"str1", | |
"str2", | |
"str3", | |
"Hello, world in item 4" | |
] | |
}; | |
const { headers, method, query, url } = req; | |
const responseBody = { headers, method, query, url, body }; | |
res.json(responseBody); | |
}) | |
app.get('/echo/:name/', (req, res) => { | |
const { name } = req.params; | |
let body = { | |
"message": `Hello, ${name}!`, | |
"items": [ | |
"str1", | |
"str2", | |
"str3" | |
] | |
}; | |
res.json(body); | |
}) | |
app.listen(port, () => { | |
console.log(`Example app running at ${port} port`); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment