-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (40 loc) · 1.25 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
var request = require('request');
var url = require('url');
var redis = require('redis');
var express = require('express');
var redisClient = redis.createClient();
var app = express.createServer();
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.get('/api/wines', function(req, response) {
rwines = redisClient.lrange('wines',0,-1,function(err, wines){
wines = wines.map(JSON.parse);
response.render('wines.ejs', {wines: wines});
});
});
app.get('/api/wines/:wine', function(req, response) {
var id = req.params.wine;
wine = redisClient.lindex('wines', id, function(err, wine){
console.log(wine);
response.render('wine.ejs', {id: id, wine: JSON.parse(wine)});
});
});
app.post('/api/wines', function(req, response) {
wine = req.body;
console.log(wine);
redisClient.rpush('wines', JSON.stringify(wine), function(err, wine){
redisClient.llen('wines', function(err, length){
id = length-1;
response.render('wine.ejs', {id: 1, wine: wine});
});
});
});
app.put('/api/wines/:wine', function(req, response) {
var id = req.params.wine;
wine = req.body;
console.log(wine);
redisClient.lset('wines', id, JSON.stringify(wine), function(err){
response.render('wine.ejs', {id: id, wine: wine});
});
});
app.listen(8080);