-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (32 loc) · 960 Bytes
/
Copy pathapp.js
File metadata and controls
44 lines (32 loc) · 960 Bytes
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
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencode = bodyParser.urlencoded({extended : false});
app.use('/', express.static('public'));
var redis = require('redis');
var client = redis.createClient();
client.select((process.env.NODE_ENV || 'development').length);
app.get('/cities', function(req, res){
client.hkeys('cities', function(error, names){
if(error) throw error;
res.json(names);
});
});
app.post('/cities', urlencode, function(req, res){
var newCity = req.body;
if(newCity.name && newCity.description){
client.hset('cities', newCity.name, newCity.description, function(error){
if(error) throw error;
res.status(201).json(newCity.name);
});
}else{
res.sendStatus(400);
}
});
app.delete('/cities/:name', function(req, res){
client.hdel('cities', req.params.name, function(err){
if(err) throw err;
res.sendStatus(204);
});
});
module.exports = app;