forked from turingschool-examples/pizza-express
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
56 lines (40 loc) · 1.35 KB
/
server.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
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const generateId = require('./lib/generate-id');
var redis = require("redis"),
client = redis.createClient('6379');
app.use(express.static('static'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'jade');
app.set('port', process.env.PORT || 3000);
app.locals.title = 'Pizza Express'
app.locals.pizzas = {};
app.get('/', (request, response) => {
response.render('index');
});
app.post('/pizzas', (request, response) => {
if (!request.body.pizza) { return response.sendStatus(400); }
var id = generateId();
var pizza = request.body.pizza;
pizza.id = id;
app.locals.pizzas[id] = pizza;
response.redirect('/pizzas/' + id);
});
app.get('/pizzas/:id', (request, response) => {
var pizza = app.locals.pizzas[request.params.id];
response.render('pizza', { pizza: pizza });
});
if (!module.parent) {
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is running on ${app.get('port')}.`);
console.log(`putting key 'somekey' with value in redis and getting it: `);
client.set("somekey", "successful test");
client.get("somekey", function(err, reply) {
// reply is null when the key is missing
console.log(reply);
});
});
}
module.exports = app;