-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-server
executable file
·133 lines (110 loc) · 3.78 KB
/
start-server
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
123
124
125
126
127
128
129
130
131
#!/usr/bin/env node
'use strict';
var argv = require('yargs').argv;
var express = require('express');
var httpProxy = require('http-proxy');
var fs = require('fs');
var https = require('https');
var path = require('path');
var url = require('url');
if (argv.h) {
console.log('Local Dev Server');
console.log('');
console.log('The local dev server starts an instance of an express webserver');
console.log('that serves static assets out of a local folder and (optionally) proxies all');
console.log('other requests to a backend server. If the `--dir` argument is not specified');
console.log('the current working directory is used.');
console.log('');
console.log('Usage:');
console.log(' start-server [<options>] <www-directory>');
console.log('');
console.log('Options:');
console.log(' --cert file\tA cert file (.crt) to use for HTTPS');
console.log(' --key file\tA key file (.pem) to use for HTTPS');
console.log(' --port num\tPort to start the webserver on');
console.log(' --proxy url\tSend all non-static requests to this address');
console.log(' --route cfg\tRespond to a dynamic route with a static file');
console.log(' \teg: `start-server --route "/signup=./public/index.html"`');
console.log('');
console.log('Environment Args:');
console.log(' Environment args will be used if available, but command line options');
console.log(' will take priority.');
console.log('');
console.log(' IQ_SERVER_PROXY\tspecify which server to proxy to');
}
// you're weird, argv.
var port = argv.port || 80;
console.log('********************************************************************');
console.log('** Server:\t http://127.0.0.1:' + port);
// figure out proxy configuration
var proxy = null;
var targetParse = null;
var proxyConfig = null;
var proxyUrl = argv.proxy || process.env.IQ_SERVER_PROXY;
if (proxyUrl) {
proxyConfig = {
target: proxyUrl
};
targetParse = url.parse(proxyUrl);
if (targetParse.protocol === 'https:') {
proxyConfig.changeOrigin = targetParse.hostname;
console.log('** Origin:\t', proxyConfig.changeOrigin);
}
console.log('** Proxying to:\t', proxyUrl);
proxy = httpProxy.createProxyServer(proxyConfig)
.on('error', function() {
// eat the error
})
.on('proxyRes', function(proxyRes) {
// secure cookies are the devil locally
var cookies = proxyRes.headers['set-cookie'];
if (cookies) {
proxyRes.headers['set-cookie'] = cookies.map(function(cookie) {
return cookie.replace(';Secure', '');
});
}
// Rewrite redirects for browser CORS checks
var location = proxyRes.headers.location;
if (location) {
proxyRes.headers.location = 'http://127.0.0.1:' + port;
}
});
}
// set up the web server
var wwwroot = path.resolve(argv._[0] || '.');
console.log('** Serving:\t', wwwroot);
var app = express();
app.use(express.static(wwwroot));
// app.use(express.logger('dev'));
// custom routes
if (argv.route) {
if (!Array.isArray(argv.route)) {
argv.route = [argv.route];
}
argv.route.forEach(function(route) {
var routeParts = route.split('=');
app.get(new RegExp(routeParts[0], 'i'), function(req, res) {
res.sendFile(routeParts[1]);
});
});
}
if (argv.proxy) {
app.use(function(req, res) {
proxy.web(req, res, proxyConfig);
});
}
// extra options
if (argv.key && argv.cert) {
console.log('** Using HTTPS');
var secureOptions = {
key: fs.readFileSync(argv.key),
cert: fs.readFileSync(argv.cert)
};
console.log(' ...Private Key: ' + argv.key);
console.log(' ...Certificate: ' + argv.cert);
app = https.createServer(secureOptions, app);
}
console.log('********************************************************************');
app.listen(port, function() {
console.log('READY');
});