-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_server.js
66 lines (57 loc) · 1.94 KB
/
static_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
const http = require('http');
const path = require('path');
const fs = require('fs');
const mimeTypes = JSON.parse(fs.readFileSync(__dirname + '/mimeTypes.json').toString());
require('./spawn_api');
const host = '0.0.0.0';
const port = 3001;
const server = http.createServer(async (request, response) => {
// Server static files
if (request.url.indexOf('.') != -1) {
const path_to_file = __dirname + '/static_files' + request.url;
fs.stat(path_to_file, (err, stat) => {
if (err || !stat.isFile()) {
response.statusCode = 404;
response.statusMessage = http.STATUS_CODES[404];
response.end();
} else {
const ext = path.extname(request.url).substr(1);
response.setHeader('Content-Type', mimeTypes[ext] || 'text/plain');
const r = fs.createReadStream(path_to_file);
r.pipe(response);
}
});
}
// Proxy to backend
else {
const proxyRequest = http.request({
port: 8080,
method: request.method,
path: request.url,
headers: request.headers
}, (proxyReq) => {
response.writeHead(proxyReq.statusCode, proxyReq.headers);
proxyReq.on('data', (chunk) => {
response.write(chunk);
});
proxyReq.on('end', () => {
response.end();
});
});
proxyRequest.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
request.on('data', (chunk) => {
proxyRequest.write(chunk);
});
request.on('end', () => {
proxyRequest.end();
});
}
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(port, host, 65535, () => {
console.log(`Server running at http://${host}:${port}/`);
});