Skip to content

Instantly share code, notes, and snippets.

@KenanBek
Last active November 4, 2020 22:23
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/69d6d3756fdb321d1d8132b32f857022 to your computer and use it in GitHub Desktop.
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/
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