forked from compstak/simple-dev-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (158 loc) · 4.63 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var path = require('path');
var fs = require('fs');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpack = require('webpack');
var express = require('express');
var send = require('send');
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
var supportsColor = require('supports-color');
var cwd = process.cwd();
var isWebpack = true;
var serverConfigs;
try {
var webpackConfigPath = path.join(cwd, 'webpack.config');
var webpackConfig = require(webpackConfigPath);
} catch (e) {
isWebpack = false;
}
try {
var serverConfigPath = path.join(cwd, 'devserver.config');
serverConfigs = require(serverConfigPath);
} catch (e) {
serverConfigs = {};
}
isWebpack = isWebpack && serverConfigs.useWebpackDevServer
if (isWebpack) {
var middlewareOptions = {
stats: {
cached: false,
cachedAssets: false,
colors: supportsColor
}
};
var webpackDevServer = webpackDevMiddleware(webpack(webpackConfig), middlewareOptions);
}
if (!Array.isArray(serverConfigs)) {
serverConfigs = [serverConfigs];
}
module.exports = serverConfigs.map(function (serverConfig) {
var devServer = express();
// use mock data if it exists
if (serverConfig.mockPath) {
console.log('Using ' + serverConfig.mockPath + ' for mock data.');
devServer.use('*', function (req, res, next) {
var mockPath = path.join(cwd, serverConfig.mockPath, req.originalUrl);
fs.stat(mockPath, function (err, stat) {
if (err) {
fs.stat(mockPath+'.json', function (err, stat) {
if (err) {
next();
return;
}
if (stat.isFile()) {
console.log('serving mock for '+req.originalUrl);
send(req, mockPath+'.json').pipe(res);
} else {
next();
}
});
return;
}
if (stat.isFile()) {
console.log('serving mock for '+req.originalUrl);
send(req, mockPath).pipe(res);
} else {
next();
}
});
});
}
if (isWebpack) {
devServer.use(webpackDevServer);
}
if (serverConfig.compressFiles) {
devServer.use(function (req, res, next) {
// current code doesn't work if the requested url has GET params (like domain.com/app.js?latest)
var toBeCompressed = serverConfig.compressFiles.find(function(ext){ return req.url.endsWith(ext); })
// if file extension is one of serverConfig.compressFiles, we send the [file].gz from hard disk
if (toBeCompressed) {
var url = req.url
if (serverConfig.publicPaths) {
var paths = Object.keys(serverConfig.publicPaths);
paths.forEach(function (path) {
url = url.replace(path + '/', '/' + serverConfig.publicPaths[path] + '/')
});
}
var fileNameOnDisc = '' + url + '.gz'
res.sendFile(fileNameOnDisc, {
root: serverConfig.projectRoot,
dotfiles: 'deny',
headers: {
'Content-Encoding': 'gzip',
'Content-Type': 'application/javascript',
}
})
} else {
next()
}
})
}
// use file if exists
if (serverConfig.publicPaths) {
var paths = Object.keys(serverConfig.publicPaths);
paths.forEach(function (path) {
console.log('Serving static data on ' + path + ' from ' + serverConfig.publicPaths[path]);
devServer.use(path, express.static(serverConfig.publicPaths[path]));
});
}
// use proxy
if (serverConfig.proxy) {
var paths = Object.keys(serverConfig.proxy);
proxy.on('error', function(e) {
console.log('Error in proxy!');
console.error(e);
});
paths.forEach(function (path) {
console.log('proxying ' + path + ' to ' + serverConfig.proxy[path]);
devServer.all(path, function (req, res, next) {
var proxyOptions = {
target: serverConfig.proxy[path]
};
proxy.web(req, res, proxyOptions, function (err) {
console.log(err.message);
if (!res.headersSent) {
res.writeHead(502, { 'content-type': 'application/json' });
}
res.end(JSON.stringify({ error: 'proxy_error', reason: err.message }));
}.bind(this));
});
});
}
if (serverConfig.app) {
serverConfig.apps = [serverConfig.app];
}
var apps = serverConfig.apps || [];
apps.forEach(function (app) {
// use app if not SPA
if (app && typeof app !== 'string') {
console.log('Using the specified express app.');
devServer.use(app);
}
// use SPA
if (app && typeof app === 'string') {
console.log('Serving ' + app + ' as your single page app.');
if (isWebpack) {
devServer.get('*', function (req, res, next) {
req.url = app;
webpackDevServer(req, res, next);
});
}
devServer.get('*', function (req, res) {
send(req, app).pipe(res);
});
}
});
devServer.set('port', serverConfig.port || 3000);
return devServer;
});