-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
90 lines (72 loc) · 1.73 KB
/
app.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
/**
* Module dependencies.
*/
var express = require('express')
, longjohn = require('longjohn')
, fs = require('fs')
, all = fs.readFileSync(__dirname + '/components.json')
, redis = require('redis')
, db = redis.createClient()
, app = module.exports = express()
, ms = require('ms')
, util = require('./util');
// middleware
app.use(express.logger());
app.use(express.responseTime());
app.use(express.compress());
/**
* Respond with packages.
*/
function reply(res) {
return function reply(err, keys) {
if (err) return res.send(500);
if (!keys.length) return res.send(404, []);
db.mget(util.componentKeys(keys), function(err, pkgs){
if (err) return res.send(500);
res.send(pkgs.map(JSON.parse));
});
}
}
/*
* CORS support.
*/
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();
});
/**
* GET all packages.
*/
app.get('/all', function(req, res){
db.incr('stats:all');
res.type('json');
res.send(all);
});
/**
* GET search :query.
*/
app.get('/search/:query', function(req, res){
var query = util.words(req.params.query);
// query stats
db.incr('stats:queries');
// word stats
query.forEach(function(word){
db.incr('stats:word:' + word);
})
// perform search
query = util.wordKeys(query);
db.sunion(query, reply(res));
});
/**
* Update all.
*/
setInterval(function(){
fs.readFile(__dirname + '/components.json', function(err, buf){
if (err) return console.log(err.stack);
console.log('updated all');
all = buf;
});
}, ms('5m'));