-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (51 loc) · 1.6 KB
/
server.js
File metadata and controls
68 lines (51 loc) · 1.6 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// const pm2 = require('pm2')
const mime = require('mime-types');
var http = require('http');
var fs = require('fs');
const url = require('url');
const build = require('./utils/build.js')
const functions = {
start,
build,
}
module.exports = async (argv) => {
try{
if (argv._.length === 1) throw new Error('No operation specified')
const operation = argv._[1]
if (!functions[operation]) throw new Error(`Unknown operation: ${operation}`)
await functions[operation](argv)
} catch (e) {
console.error(e.message || e)
process.exit(1)
}
}
const path = require('path');
function startServer(argv) {
const { port = 9615 } = argv || {}
const index = fs.readFileSync(path.join(__dirname, '..', 'dist', 'index.html'));
http.createServer(function (req, res) {
try {
const parsedUrl = url.parse(req.url, true);
// verify if url is a file in dist folder
if (parsedUrl.pathname === '/') throw {}
let filePath = path.join(__dirname, '..', 'dist', parsedUrl.pathname);
if (fs.existsSync(filePath)) {
const contentType = mime.lookup(filePath) || 'text/plain';
res.writeHead(200, { 'Content-Type': contentType });
res.end(fs.readFileSync(filePath));
return
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(index);
} catch {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(index);
}
}).listen(port);
console.log('🚀 Server start')
console.log('🚀 Server listening on port ' + port)
}
async function start(argv) {
await build()
startServer(argv)
}