-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
54 lines (37 loc) · 1.12 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
'use strict';
var http = require('http');
global.fetch = require('node-fetch');
global.Promise = require('bluebird');
global.fetch.Promise = global.Promise;
var staticServer = require('./file-server');
var proxyCw = require('./proxy-cw');
var assign = require('./Object.assign');
var defaultConfig = {
proxyHostname: 'localhost',
proxyPort: '8080',
proxyRe: /^\/cw/,
dataRe: /^\/data/,
publicPath: 'public'
};
function start(config, otherUrlCb) {
config = assign({}, defaultConfig, config);
proxyCw.configure(config);
staticServer.configure(config);
function onRequest(req, res) {
console.log('url', req.url);
if (config.dataRe.test(req.url)) {
console.log('\tdata url => serve file');
staticServer(req, res);
return;
} else if (config.proxyRe.test(req.url)) {
console.log('\tcw url => proxy');
proxyCw(req, res);
return;
} else {
otherUrlCb(req, res);
}
}
http.createServer(onRequest).listen(3000);
console.log('listening on 3000');
}
exports.start = start;