-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (46 loc) · 1.49 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
'use strict';
const http = require('http');
const url = require('url');
const fs = require('fs');
const path = require('path');
const bundleDir = path.resolve(__dirname, 'dist');
const publicDir = path.resolve(__dirname, 'public');
const index = path.join(bundleDir, 'index.html');
function serveFile(res, filepath) {
return new Promise((resolve, reject) => {
console.log(filepath);
fs.createReadStream(filepath)
.on('error', reject)
.pipe(res)
.on('error', reject)
.on('finish', resolve);
});
}
function serve(res, contentBase, pathname) {
const filepath = path.join(contentBase[0], pathname);
contentBase = contentBase.slice(1);
return serveFile(res, filepath).catch(err => {
if (!(err.code === 'ENOENT' || err.code === 'EISDIR')) {
res.statusCode = 500;
res.end(err.message, 'utf8');
return;
}
if (contentBase.length > 0) {
return serve(res, contentBase, pathname);
}
// Fallback to SPA router
return serveFile(res, index);
});
}
http.createServer(function requestListener(req, res) {
let { pathname } = url.parse(req.url);
console.log(`${req.method} ${pathname}`);
if (req.method !== 'GET') {
res.statusCode = 501;
return res.end();
}
if (pathname === '/') {
pathname = '/index.html';
}
return serve(res, [bundleDir, publicDir], pathname);
}).listen(process.argv[2] || '8080');