forked from binocarlos/moby-counter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
71 lines (57 loc) · 1.77 KB
/
server.js
File metadata and controls
71 lines (57 loc) · 1.77 KB
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
var concat = require('concat-stream')
var Router = require('routes-router')
var ecstatic = require('ecstatic')
var redis = require('redis')
module.exports = function(opts){
console.log('using redis server')
var port = opts.redis_port || process.env.USE_REDIS_PORT || 6379
var host = opts.redis_host || process.env.USE_REDIS_HOST || 'redis'
var connectionStatus = false
var client = redis.createClient(port, host, {})
client.on('error', function(err){
connectionStatus = false
console.log('Error from the redis connection:')
console.log(err)
})
client.on('end', function(err){
connectionStatus = false
console.log('Lost connection to Redis server')
})
client.on('ready', function(err){
connectionStatus = true
console.log('Connection made to the Redis server')
})
console.log('-------------------------------------------');
console.log('have host: ' + host)
console.log('have port: ' + port)
var router = Router()
var fileServer = ecstatic({ root: __dirname + '/client' })
router.addRoute("/v1/ping", {
GET: function(req, res){
res.setHeader('Content-type', 'application/json')
res.end(JSON.stringify({
connected:connectionStatus
}))
}
})
router.addRoute("/v1/whales", {
GET: function (req, res) {
client.lrange('whales', 0, -1, function(err, data){
res.setHeader('Content-type', 'application/json')
res.end(JSON.stringify(data))
})
},
POST: function (req, res) {
req.pipe(concat(function(data){
data = data.toString()
client.rpush('whales', data, function(){
client.save(function(){
res.end('ok')
})
})
}))
}
})
router.addRoute("/*", fileServer)
return router
}