-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·67 lines (57 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
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env node
// EDIT THESE FOR SSL SUPPORT
const KEY_PATH = './ privkey.pem';
const CERT_PATH = './cert.pem';
('use strict');
var http = require('http'),
https = require('https'),
send = require('send'),
fs = require('fs'),
url = require('url');
const options = {};
let server = {};
try {
let key = fs.readFileSync(KEY_PATH);
let cert = fs.readFileSync(CERT_PATH);
options.key = key;
options.cert = cert;
server = https;
} catch (error) {
server = http;
}
var app = server.createServer(options, function(req, res) {
function error(err) {
res.statusCode = err.status || 500;
console.log('%s: %s', err.status, req.url);
res.end(err.message);
}
function redirect() {
res.statusCode = 301;
res.setHeader('Location', req.url + '/');
res.end('Redirecting to ' + req.url + '/');
}
send(req, url.parse(req.url).pathname, {
root: process.cwd(),
})
.on('error', error)
.on('directory', redirect)
.on('file', function() {
console.log('%s: %s', res.statusCode, req.url);
})
.pipe(res);
});
var port = process.argv[2] || 3333;
console.log('HTTP Static Listening on port %s', port);
app.on('error', function(e) {
if (e.code === 'EADDRINUSE') {
console.log('Address in use, incrementing and retrying...');
setTimeout(function() {
// app.close();
port++;
app.listen(port, function() {
console.log('HTTP Static Listening on port %s', port);
});
}, 1000);
}
});
app.listen(port);