-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.js
61 lines (49 loc) · 1.34 KB
/
router.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
var
config = require('./config'),
help = require('./routes/help'),
components = require('./routes/components'),
mocks = require('./routes/mocks'),
logger = require('./lib/logger');
module.exports = function (app) {
app.all('*', function(req, res, next){
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
if ('OPTIONS' == req.method) return res.send(200);
next();
});
/**
* Tracking
*/
app.all('*', function (req, res, next) {
if (/^\/components/.test(req.url)) {
logger.track(req.method.toUpperCase() + ': ' + req.url);
}
next();
});
/**
* Landing page resource
*/
app.get('/', function (req, res) {
res.json(help);
});
/**
* API
*/
app.get('/components', components.index);
app.get('/components/:owner/:name', components.show);
app.get('/components/:owner/:name/build.js', components.getBuild);
/**
* Error handling
*/
app.use(function (err, req, res, next) {
logger.debug.error(req.method.toUpperCase() + ': ' + req.url, err);
res.send(err.code || 500, { error: err.message });
});
/**
* Mocks in test and/or dev
*/
if (config.useMockRegistry) {
app.get('/mock/registry', mocks.registry);
}
};