-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·92 lines (84 loc) · 2.56 KB
/
Copy pathapp.js
File metadata and controls
executable file
·92 lines (84 loc) · 2.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const http = require('http');
const https = require('https');
const path = require('path');
const cert = fs.readFileSync('./ssl/vaultfinance_net.crt');
const ca = fs.readFileSync('./ssl/vaultfinance_net.ca-bundle');
const key = fs.readFileSync('./ssl/vaultfinance_net.key');
var options = {
cert: cert,
ca: ca,
key: key
};
args = process.argv;
if (args.length > 2) {
if (args[2] == 'dev') {
hostname = 'localhost';
port = 443;
} else if (args[2] == 'dist'){
hostname = 'vaultfinance.net';
port = 443;
} else {
console.log('invalid arg, use dist setting\n');
hostname = 'vaultfinance.net';
port = 443;
}
} else {
console.log('invalid arg, use dist setting\n');
hostname = 'vaultfinance.net';
port = 443;
}
console.log('https listen');
const httpsServer = https.createServer(options, (req, res) => {
console.log(req.url);
url = req.url.split('?')[0];
var filePath = './public' + url;
if (url == '/') {
filePath = './public/index.html';
}
var extname = String(path.extname(filePath)).toLowerCase();
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.wav': 'audio/wav',
'.mp4': 'video/mp4',
'.woff': 'application/font-woff',
'.ttf': 'application/font-ttf',
'.eot': 'application/vnd.ms-fontobject',
'.otf': 'application/font-otf',
'.wasm': 'application/wasm'
};
var contentType = mimeTypes[extname] || 'application/octet-stream';
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT') {
fs.readFile('./404.html', function(error, content) {
res.writeHead(404, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
});
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
}
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
});
});
httpsServer.listen(port, hostname);
console.log('http listen');
const httpServer = http.createServer((req, res) => {
res.statusCode = 301;
res.setHeader('Location', `https://${hostname}${req.url}`);
res.end();
});
httpServer.listen(80);