-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (65 loc) · 2.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const http = require('http');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const { logRequest } = require('./utils/logger');
const { loadArticles, saveArticles } = require('./utils/fileHandler');
const articleHandlers = require('./handlers/articleHandlers');
const commentHandlers = require('./handlers/commentHandlers');
const hostname = '127.0.0.1';
const port = 3000;
let articles = [];
const handlers = {
'/api/articles/readall': articleHandlers.readAll,
'/api/articles/read': articleHandlers.read,
'/api/articles/create': articleHandlers.create,
'/api/articles/update': articleHandlers.update,
'/api/articles/delete': articleHandlers.delete,
'/api/comments/create': commentHandlers.create,
'/api/comments/delete': commentHandlers.delete,
};
const server = http.createServer((req, res) => {
parseBodyJson(req, (err, payload) => {
const handler = getHandler(req.url);
logRequest(req, payload);
handler(req, res, payload, articles, (err, result) => {
if (err) {
res.statusCode = err.code;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(err));
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(result));
});
});
});
function getHandler(url) {
return handlers[url] || notFound;
}
function notFound(req, res, payload, cb) {
cb({ code: 404, message: 'Not found' });
}
function parseBodyJson(req, cb) {
let body = [];
req.on('data', (chunk) => body.push(chunk))
.on('end', () => {
try {
body = Buffer.concat(body).toString();
cb(null, JSON.parse(body));
} catch {
cb({ code: 400, message: 'Invalid JSON' });
}
});
}
loadArticles('./articles.json', (err, data) => {
if (err) {
console.error('Error loading articles:', err.message);
process.exit(1);
}
articles = data;
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
});