forked from lib-ruby-parser/wasm-bindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
55 lines (48 loc) · 1.51 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
const http = require('http');
var fs = require('fs');
var path = require('path');
let server = null;
const root = path.join(__filename, '../..');
function startServer(options = {}) {
const log = options.logging ? console.log : () => { };
log(options);
const port = options.port || 8080;
server = http.createServer((req, res) => {
log(`req.url = ${req.url}`);
let filePath;
if (req.url === '/' || req.url == '/favicon.ico') {
filePath = path.join(root, 'tests/index.html');
} else {
filePath = path.join(root, 'build' + req.url);
}
let extname = path.extname(filePath);
let contentType;
switch (extname) {
case '.js':
contentType = 'text/javascript';
break;
case '.wasm':
contentType = 'application/wasm'
break;
case '.html':
case '.ico':
contentType = 'text/html';
break;
default:
throw `Unsupported extension ${extname}`;
}
log(`returning file ${filePath} with content-type ${contentType}`);
fs.readFile(filePath, (err, content) => {
if (err) {
throw err;
}
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
});
});
server.listen(port);
}
function stopServer() {
server.close();
}
module.exports = { startServer, stopServer };