-
Notifications
You must be signed in to change notification settings - Fork 1
/
web.js
48 lines (46 loc) · 2.02 KB
/
web.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
var connect = require('connect'),
jsonp = require('connect-jsonp'),
port = process.env.PORT || 5000,
redis_url = process.env.REDISTOGO_URL || 'redis://localhost:6379',
redis = require("redis-url").createClient(redis_url)
var valid = function(k){
return k.replace(/[ ?\/]/g,'').substring(0,250)
}
connect.createServer(
connect.logger({ format: ':method :url' }),
connect.responseTime(),
connect.static(__dirname + '/public'),
jsonp(),
connect.errorHandler({ dumpExceptions: true, showStack: true }),
connect.router(function(app){
app.get('/rasta.min.js', function(req, res, next){
res.writeHead(200, {'Content-Type': 'text/javascript'})
res.end("(function(d,w,r,j){j=function(a,b,e,c,s){d.body.appendChild(s=d.createElement(c='script')).src=r.url+a+(c=c+(j.i=-~j.i));e&&(s.onerror=e);(a=window)[c]=function(v){b(v);delete a[c];d.body.removeChild(s)}};window.Rasta=r={url:'http://rastajs.errorjs.com',valid:function(k){return k.replace(/[ ?\/]/g,'').substring(0,250)},ts:function(){return parseInt(new Date().getTime() / 1000)},get:function(k,c,e){return j('/get/'+this.valid(k)+'?ts='+this.ts()+'&callback=',c,e)},set:function(k,v,c,e){return j('/set/'+this.valid(k)+'/'+this.valid(v)+'?ts='+this.ts()+'&callback=',c,e)}}})(document,window)")
})
app.get('/get/:key', function(req, res, next){
var key = valid(req.params.key)
redis.get(key, function(err, val){
if(err){
console.log('err:'+err)
connect.utils.badRequest(res)
} else {
res.writeHead(200, {'Content-Type': 'text/json'});
res.end(JSON.stringify(val || ''))
}
})
})
app.get('/set/:key/:val', function(req, res, next){
var key = valid(req.params.key),
val = valid(req.params.val)
redis.set(key, val, function(err){
if(err){
console.log(err)
connect.utils.badRequest(res)
} else {
res.writeHead(200, {'Content-Type': 'text/text'})
res.end()
}
})
})
})
).listen(port)