-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (45 loc) 路 1.36 KB
/
Copy pathserver.js
File metadata and controls
50 lines (45 loc) 路 1.36 KB
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
#!/usr/bin/env node
/**
* Static server with COOP/COEP headers required for SharedArrayBuffer.
* Run: node server.js
* Then open http://localhost:8765
*/
var http = require('http');
var fs = require('fs');
var path = require('path');
var PORT = process.env.PORT || 8765;
var ROOT = path.resolve(__dirname);
var MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpeg',
'.ico': 'image/x-icon',
'.svg': 'image/svg+xml'
};
var server = http.createServer(function (req, res) {
var url = req.url.split('?')[0];
if (url === '/') url = '/index.html';
var file = path.join(ROOT, url);
fs.readFile(file, function (err, data) {
if (err) {
res.writeHead(404);
res.end('Not found');
return;
}
var ext = path.extname(file);
var contentType = MIME[ext] || 'application/octet-stream';
res.writeHead(200, {
'Content-Type': contentType,
'Cross-Origin-Opener-Policy': 'same-origin',
'Cross-Origin-Embedder-Policy': 'credentialless'
});
res.end(data);
});
});
server.listen(PORT, function () {
console.log('Server at http://localhost:' + PORT);
console.log('COOP/COEP headers enabled for SharedArrayBuffer');
});