Last active
November 4, 2020 22:23
-
-
Save KenanBek/69d6d3756fdb321d1d8132b32f857022 to your computer and use it in GitHub Desktop.
Simple JSON API using Node.js and Express.js. 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) => { | |
res.setHeader('Content-Type', 'application/json'); | |
let body = { | |
"message": "Hello, world!", | |
"items": [ | |
"str1", | |
"str2", | |
"str3" | |
] | |
}; | |
const { headers, method, query, url } = req; | |
const responseBody = { headers, method, query, url, body }; | |
res.send(JSON.stringify(responseBody)); | |
}) | |
app.listen(port, hostname, () => { | |
console.log(`Example app listening at http://${hostname}:${port}`); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment