-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathdevserver.js
More file actions
122 lines (113 loc) · 3.22 KB
/
Copy pathdevserver.js
File metadata and controls
122 lines (113 loc) · 3.22 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var express = require('express'),
parseUrl = require('parseurl'),
path = require('path'),
http = require('http'),
https = require('https'),
url = require('url'),
tamper = require('tamper'),
serverbase = require('./serverbase.js'),
config = require('./config'),
httpProxy = require('http-proxy'),
app = module.exports = express(),
proxy = new httpProxy.createProxyServer({});
proxy.on('error', function() {
});
function rewriteRules(req, res, next) {
var u = parseUrl(req);
if (req.path == '/') {
u.pathname = '/welcome.html';
} else if (/^\/edit\//.test(req.path)) {
if (/^frame\./.test(req.headers.host)) {
u.pathname = '/framed.html';
} else {
u.pathname = '/editor.html';
}
} else if (/^\/home(?=\/).*\/$/.test(req.path)) {
u.pathname = '/dir.html';
} else {
next();
return;
}
req.url = url.format(u);
next();
}
var expandSiteInclude = tamper(function(req, res) {
if (!/^(?:(?:https?:)?\/\/[^\/]+)?\/[^\/]*\.html$/i.test(req.url)) {
return;
}
return function(body) {
if (res.locals.site) {
body = body.replace(/<!--#echo var="site"-->/g,
res.locals.site + res.locals.portpart);
}
if (res.locals.filepath) {
body = body.replace(/<!--#echo var="filepath"-->/g, res.locals.filepath);
}
return body;
}
});
function noAppcache(req, res, next) {
if (/\.appcache$/.test(req.path)) {
res.status(404).send()
} else {
next();
}
}
function proxyRules(req, res, next) {
var exp = (req.app.locals.config.useProxy) ?
/^\/(?:code|home|load|save|proxy|img|link|print|thumb|socket\.io)(?=\/)/
: /^\/(?:proxy)(?=\/)/;
if (exp.test(req.path) && /\.dev$/.test(req.hostname)) {
var host = req.headers['host'] = req.hostname.replace(/\.dev$/, '');
req.headers['url'] = req.path;
proxy.web(req, res, {
target: { host: host, port: 80 },
xfwd: true
});
} else {
next();
}
}
function proxyPacGenerator(req, res, next) {
if (req.path == '/proxy.pac') {
var hostHeader = req.get('host'),
hostMatch = /^([^:]+)(?::(\d*))?$/.exec(hostHeader || ''),
hostDomain = (hostMatch && hostMatch[1]) || 'localhost',
hostPort = (hostMatch && hostMatch[2]) || process.env.PORT;
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end(
'function FindProxyForURL(url, host) {\n' +
' if (shExpMatch(host, "*.dev") || ' +
'host == "drive.pencilcode.net") {\n' +
' return "PROXY ' + hostDomain + ':' + hostPort + '";\n' +
' }\n' +
' return "DIRECT";\n' +
'}\n'
);
} else {
next();
}
}
serverbase.initialize(app);
app.use(rewriteRules);
app.use(expandSiteInclude);
app.use(noAppcache);
app.use(proxyRules);
app.use(proxyPacGenerator);
serverbase.initialize2(app);
var server = false;
if (app.locals.config.useHttps) {
var fs = require('fs')
var options = {
key: fs.readFileSync('server/devserver-key.pem'),
cert: fs.readFileSync('server/devserver-cert.pem')
};
server = https.createServer(options, app);
} else {
server = http.createServer(app);
}
server.listen(process.env.PORT, function() {
console.log('Express server listening on ' + process.env.PORT);
});