Skip to content

Instantly share code, notes, and snippets.

@KenanBek
Created November 8, 2020 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KenanBek/daa4fe9d9ca273cf7afab9c602c20252 to your computer and use it in GitHub Desktop.
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/
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