-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweaver-webserver.js
64 lines (51 loc) · 1.54 KB
/
weaver-webserver.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
var fs = require('fs');
var process = require('process');
var path = require('path');
var express = require('express');
var https = require('https');
var app = express();
var router = express.Router();
var args = process.argv.slice(2, process.argv.length);
var port = args[0] ? args[0] : 8080;
var base = args[1] ? args[1] : '/';
var root = process.cwd() + path.sep + 'dist';
router.use(express.static('dist'));
router.use(function (req, res, next) {
if (req.get("X-Requested-With")) {
res.status(404).send('Not found');
throw new Error(getTime() + ": Resource requested and not found at -- " + req.url);
} else {
res.sendFile("index.html", {
root: root
});
}
});
app.use(base, router);
var sslCert = "ssl.crt";
var sslKey = "ssl.key";
var useSSL = true;
try {
fs.accessSync(sslCert, fs.constants.R_OK);
fs.accessSync(sslKey, fs.constants.R_OK);
} catch (err) {
useSSL = false;
}
if (useSSL) {
var credentials = {
cert: fs.readFileSync(sslCert),
key: fs.readFileSync(sslKey)
};
var ssl = https.createServer(credentials, app);
ssl.listen(port, function () {
console.log(getTime() + ': Listening with SSL on port ' + port + ' at ' + base);
});
} else {
app.listen(port, function () {
console.log(getTime() + ': Listening on port ' + port + ' at ' + base);
});
}
function getTime() {
return new Date().toISOString().
replace(/T/, ' '). // replace T with a space
replace(/\..+/, ''); // delete the dot and everything after
}