Chapter 4: initial recipe samples

This commit is contained in:
Beth Griggs
2020-04-29 22:00:34 +01:00
parent 503edc4ec2
commit bcf782f7dc
59 changed files with 471 additions and 655 deletions
+28
View File
@@ -0,0 +1,28 @@
const http = require('http')
const HOSTNAME = process.env.HOSTNAME || '0.0.0.0'
const PORT = process.env.PORT || 8080
const server = http.createServer((req, res) => {
if (req.method !== 'GET') return error(res, 405)
if (req.url === '/todo') return todo(res)
if (req.url === '/') return index(res)
error(res, 404)
});
function error(res, code) {
res.statusCode = code
res.end(`{"error": "${http.STATUS_CODES[code]}"}`)
}
function todo(res) {
res.end('[{"task_id": 1, "description": "walk dog"}]}')
}
function index(res) {
res.end('{"name": "todo-server"}')
}
server.listen(PORT, HOSTNAME, () => {
console.log('Server listening on', server.address())
})