-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (32 loc) · 996 Bytes
/
index.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
const { program } = require('commander');
const http = require('http');
const https = require('https');
const fs = require('fs');
program
.option('--location <url>', 'redirect location')
.option('--http <port>', 'http server port')
.option('--https <port>', 'https server port')
.option('--key <path>', 'https key path')
.option('--cert <path>', 'https cert path')
program.parse(process.argv);
console.log(program.opts());
if (!program.location) {
throw new Error('--location must be set');
}
if (program.http) {
http.createServer((_req, res) => {
res.writeHead(301, {'Location': program.location});
res.end();
}).listen(program.http);
}
if (program.https) {
const options = {};
if (program.key && program.cert) {
options.key = fs.readFileSync(program.key);
options.cert = fs.readFileSync(program.cert);
}
https.createServer(options, (_req, res) => {
res.writeHead(301, {'Location': program.location});
res.end();
}).listen(program.https);
}