UNPKG

1.56 kBJavaScriptView Raw
1'use strict';
2
3var koa = require('koa');
4var app = module.exports = koa();
5var http = require('http');
6var middlewares = require('koa-middlewares');
7var routes = require('../routes/registry');
8var logger = require('../common/logger');
9var config = require('../config');
10var block = require('../middleware/block');
11var auth = require('../middleware/auth');
12var staticCache = require('../middleware/static');
13var notFound = require('../middleware/registry_not_found');
14var cors = require('kcors');
15var proxyToNpm = require('../middleware/proxy_to_npm');
16var maxrequests = require('koa-maxrequests');
17
18app.use(maxrequests());
19app.use(block());
20middlewares.jsonp(app);
21app.use(middlewares.rt({ headerName: 'X-ReadTime' }));
22app.use(middlewares.rewrite('/favicon.ico', '/favicon.png'));
23app.use(staticCache);
24
25app.keys = ['todokey', config.sessionSecret];
26app.proxy = true;
27app.use(middlewares.bodyParser({ jsonLimit: config.jsonLimit }));
28app.use(cors({
29 allowMethods: 'GET,HEAD',
30}));
31app.use(auth());
32app.use(proxyToNpm());
33app.use(notFound);
34
35if (config.enableCompress) {
36 app.use(middlewares.compress({ threshold: 150 }));
37}
38app.use(middlewares.conditional());
39app.use(middlewares.etag());
40
41/**
42 * Routes
43 */
44
45app.use(middlewares.router(app));
46routes(app);
47
48/**
49 * Error handler
50 */
51
52app.on('error', function (err, ctx) {
53 console.log(err);
54 console.log(err.stack);
55 err.url = err.url || ctx.request.url;
56 logger.error(err);
57});
58
59app = http.createServer(app.callback());
60
61if (!module.parent) {
62 app.listen(config.registryPort);
63}
64
65module.exports = app;