This repository was archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathhttp-server.ts
More file actions
151 lines (125 loc) · 4.63 KB
/
http-server.ts
File metadata and controls
151 lines (125 loc) · 4.63 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import * as path from 'path';
import { injectNotificationScript } from './injector';
import { injectLiveReloadScript } from './live-reload';
import * as express from 'express';
import * as fs from 'fs';
import * as url from 'url';
import {
ServeConfig,
LOGGER_DIR,
IONIC_LAB_URL,
IOS_PLATFORM_PATH,
ANDROID_PLATFORM_PATH
} from './serve-config';
import { Logger } from '../logger/logger';
import * as proxyMiddleware from 'proxy-middleware';
import { injectDiagnosticsHtml } from '../logger/logger-diagnostics';
import * as Constants from '../util/constants';
import { getBooleanPropertyValue } from '../util/helpers';
import { getProjectJson, IonicProject } from '../util/ionic-project';
import { LabAppView, ApiCordovaProject, ApiPackageJson } from './lab';
/**
* Create HTTP server
*/
export function createHttpServer(config: ServeConfig): express.Application {
const app = express();
app.set('serveConfig', config);
app.listen(config.httpPort, config.host, function() {
Logger.debug(`listening on ${config.httpPort}`);
});
app.get('/', serveIndex);
app.use('/', express.static(config.wwwDir));
app.use(`/${LOGGER_DIR}`, express.static(path.join(__dirname, '..', '..', 'bin'), { maxAge: 31536000 }));
// Lab routes
app.use(IONIC_LAB_URL + '/static', express.static(path.join(__dirname, '..', '..', 'lab', 'static')));
app.get(IONIC_LAB_URL, LabAppView);
app.get(IONIC_LAB_URL + '/api/v1/cordova', ApiCordovaProject );
app.get(IONIC_LAB_URL + '/api/v1/app-config', ApiPackageJson);
app.get('/cordova.js', servePlatformResource, serveMockCordovaJS);
app.get('/cordova_plugins.js', servePlatformResource);
app.get('/plugins/*', servePlatformResource);
if (config.useProxy) {
setupProxies(app);
}
if (config.isPathLocationStrategy) {
app.get('*', serveIndex);
}
return app;
}
function setupProxies(app: express.Application) {
if (getBooleanPropertyValue(Constants.ENV_READ_CONFIG_JSON)) {
getProjectJson().then(function(projectConfig: IonicProject) {
for (const proxy of projectConfig.proxies || []) {
let opts: any = url.parse(proxy.proxyUrl);
if (proxy.proxyNoAgent) {
opts.agent = false;
}
opts.rejectUnauthorized = !(proxy.rejectUnauthorized === false);
opts.cookieRewrite = proxy.cookieRewrite;
app.use(proxy.path, proxyMiddleware(opts));
Logger.info('Proxy added:' + proxy.path + ' => ' + url.format(opts));
}
}).catch((err: Error) => {
Logger.error(`Failed to read the projects ionic.config.json file: ${err.message}`);
});
}
}
/**
* http responder for /index.html base entrypoint
*/
function serveIndex(req: express.Request, res: express.Response) {
const config: ServeConfig = req.app.get('serveConfig');
// respond with the index.html file
const indexFileName = path.join(config.wwwDir, process.env[Constants.ENV_VAR_HTML_TO_SERVE]);
fs.readFile(indexFileName, (err, indexHtml) => {
if (!indexHtml) {
Logger.error(`Failed to load index.html`);
res.send('try again later');
return;
}
if (config.useLiveReload) {
indexHtml = injectLiveReloadScript(indexHtml, req.hostname, config.liveReloadPort);
indexHtml = injectNotificationScript(config.rootDir, indexHtml, config.notifyOnConsoleLog, config.notificationPort);
}
indexHtml = injectDiagnosticsHtml(config.buildDir, indexHtml);
res.set('Content-Type', 'text/html');
res.send(indexHtml);
});
}
/**
* http responder for cordova.js file
*/
function serveMockCordovaJS(req: express.Request, res: express.Response) {
res.set('Content-Type', 'application/javascript');
res.send('// mock cordova file during development');
}
/**
* Middleware to serve platform resources
*/
function servePlatformResource(req: express.Request, res: express.Response, next: express.NextFunction) {
const config: ServeConfig = req.app.get('serveConfig');
const userAgent = req.header('user-agent');
let resourcePath = config.wwwDir;
if (!config.isCordovaServe) {
return next();
}
if (isUserAgentIOS(userAgent)) {
resourcePath = path.join(config.rootDir, IOS_PLATFORM_PATH);
} else if (isUserAgentAndroid(userAgent)) {
resourcePath = path.join(config.rootDir, ANDROID_PLATFORM_PATH);
}
fs.stat(path.join(resourcePath, req.url), (err, stats) => {
if (err) {
return next();
}
res.sendFile(req.url, { root: resourcePath });
});
}
function isUserAgentIOS(ua: string) {
ua = ua.toLowerCase();
return (ua.indexOf('iphone') > -1 || ua.indexOf('ipad') > -1 || ua.indexOf('ipod') > -1);
}
function isUserAgentAndroid(ua: string) {
ua = ua.toLowerCase();
return ua.indexOf('android') > -1;
}