-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
51 lines (49 loc) · 1.65 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var http=require('http');
var httpProxy=require('http-proxy');
var proxy=new httpProxy.createProxyServer({ws:true,target:{host:'localhost',port:8080}});
var fs=require('fs');
var path=require('path');
var showdown=require('showdown');
var converter = new showdown.Converter();
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif'
};
http.createServer(function (req, res) {
var convertReadme=false;
if (req.headers.host=='global.minesnf.com' || req.headers.host=='ru1.minesnf.com') {
proxy.web(req, res);
} else {
var filePath=path.join(__dirname,req.url=="/"?'index.html':req.url);
var extname = path.extname(filePath).toLowerCase();
var contentType = mimeTypes[extname] || 'text/plain';
if (path.basename(filePath).toLowerCase()=='index.html'){
convertReadme=true;
filePath=path.join(__dirname,"readme.md");
}
fs.readFile(filePath, function(error, content) {
if (error) {
if(error.code == 'ENOENT') {
fs.readFile(path.join(__dirname,'404.html'), function(error, content) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(content, 'utf-8');
});
} else {
res.writeHead(500);
res.end(error.code);
res.end();
}
} else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(convertReadme?converter.makeHtml(content.toString()):content, 'utf-8');
}
});
}
}).on('upgrade', function (req, socket, head) {
proxy.ws(req, socket, head);
}).listen(80);