-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
68 lines (62 loc) · 1.9 KB
/
server.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
const http = require('http');
const fs = require('fs');
const utils = require('./src/utils');
const users = require('./src/users');
const menu = require('./src/menu');
const shoppingCart = require('./src/shopping-cart');
const order = require('./src/order');
function serverHandler(req, res) {
utils.log(`${req.method} ${req.url}`);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Request-Method', '*');
res.setHeader('Access-Control-Allow-Methods', 'OPTIONS, GET');
res.setHeader('Access-Control-Allow-Headers', '*');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
if (
req.url.startsWith('/users') ||
req.url.startsWith('/menu') ||
req.url.startsWith('/shopping-cart') ||
req.url.startsWith('/order') ||
req.url.startsWith('/cli')
) {
if (req.url.startsWith('/users')) {
utils.log('routing to users handler');
users.serverHandler(req, res);
utils.log('Finished users handler');
} else if (req.url.startsWith('/menu')) {
menu.serverHandler(req, res);
} else if (req.url.startsWith('/shopping-cart')) {
shoppingCart.serverHandler(req, res);
} else if (req.url.startsWith('/order')) {
order.serverHandler(req, res);
} else if (req.url.startsWith('/cli')) {
cli.serverHandler(req, res);
}
} else if (req.method === 'GET') {
respondWithFile(res, '/index.html');
} else {
res.statusCode = 404;
res.write('Not found');
res.end();
}
}
function respondWithFile(res, path) {
const filePath = __dirname + '/frontend/build' + path;
utils.log(`📁 Responding with file: ${filePath}`);
fs.readFile(filePath, (err, data) => {
if (err) {
utils.log(`❌ Error reading file: ${err}`);
return;
}
res.write(data);
res.end();
});
}
function init() {
http.createServer(serverHandler).listen(3333);
}
module.exports = { init };