Last active
November 4, 2020 22:23
Star
You must be signed in to star a gist
Simple JSON API in Node.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 http = require('http'); | |
const hostname = '127.0.0.1'; | |
const port = 3000; | |
const server = http.createServer((req, res) => { | |
res.statusCode = 200; | |
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.write(JSON.stringify(responseBody)); | |
res.end(); | |
}).listen(port, hostname, () => { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment