Skip to content

Instantly share code, notes, and snippets.

@KenanBek

KenanBek/server.js

Created Nov 8, 2020
Embed
What would you like to do?
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