forked from browserify/wzrd.in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (47 loc) · 1.49 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
'use strict';
const http = require('http');
const _ = require('lodash');
const express = require('express');
const minilog = require('minilog');
const Bundler = require('./lib/bundler');
const config = require('./config');
const routes = require('./routes');
const app = express();
const bundler = new Bundler(config);
const router = new express.Router();
routes(router, bundler, config);
app.use(function(req, res, next) {
if (/nnn\.ed\.jp/.test(req.headers['referer'])) {
res.statusCode = 429
return res.end('too many requests');
}
next();
});
app.use(require('cors')(config.cors));
app.use(require('compression')());
app.use(require('./middlewares/request-logger'));
app.use(router);
app.use(express.static(__dirname + '/public'));
function start(callback) {
callback = callback || ((err) => { if (err) { throw err; } });
const log = minilog('browserify-cdn');
minilog
.pipe(minilog.backends.console.formatNpm)
.pipe(minilog.backends.console)
;
bundler.init().then(() => {
const server = http.createServer(app).listen(_.get(config, 'server.port'), function (err) {
if (err) return callback(err);
const addr = server.address();
log.info('browserify-cdn is online');
log.info('http://' + addr.address + ':' + addr.port);
callback(null, server);
});
}, (err) => {
callback(err);
}, (err) => setImmediate(() => { throw err; }));
}
exports.start = start;
exports.app = app;
exports.bundler = bundler;
exports.config = config;