Chapter 4,6: prettier updates and web framework samples

This commit is contained in:
Beth Griggs
2020-05-31 19:31:42 +01:00
parent b43bf2c378
commit 0d8170c265
43 changed files with 4820 additions and 242 deletions
+13 -13
View File
@@ -1,28 +1,28 @@
const http = require('http')
const http = require("http");
const HOSTNAME = process.env.HOSTNAME || '0.0.0.0'
const PORT = process.env.PORT || 8080
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)
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]}"}`)
res.statusCode = code;
res.end(`{"error": "${http.STATUS_CODES[code]}"}`);
}
function todo(res) {
res.end('[{"task_id": 1, "description": "walk dog"}]}')
res.end('[{"task_id": 1, "description": "walk dog"}]}');
}
function index(res) {
res.end('{"name": "todo-server"}')
res.end('{"name": "todo-server"}');
}
server.listen(PORT, HOSTNAME, () => {
console.log('Server listening on', server.address())
})
console.log("Server listening on", server.address());
});