-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (60 loc) · 2 KB
/
index.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
57
58
59
60
61
62
63
64
65
66
67
68
69
var express = require('express')
var app = express()
var cloudCache = require('./src/cache.js')
app.set('port', (process.env.PORT || 8080))
app.use(express.static(__dirname + '/public'))
app.get('/', function(request, response) {
response.send('Hello World!')
})
app.get('/pizzas', function(request, response) {
response.send('<b>Lets Order Some Pizza <br/></b>'
+ '-------------------------------'
+ '<br/>'
+ '<h3>types: plain, fancy</h3>'
+ '<br/>'
+ 'GET /orderPizza?email={emailId}&type={pizzaType} - Order a pizza <br/>'
+ 'GET /orders?email={emailId} - get specific value <br/>')
})
app.get('/orderpizza', (request, response) => {
// console.log('email: ' + request.query.email);
// console.log('type: ' + request.query.type);
var pizzaInFlight = createPizza(request.query.type);
console.log('pizza: ' + pizzaInFlight);
cloudCache.addToCache(request.query.email, pizzaInFlight);
response.send('Succesfully Placed Order For: ' + request.query.email);
})
function createPizza( pizzaType) {
var pizza;
if (pizzaType.toString().trim() === 'plain') {
pizza = {
type: 'plain',
toppings: ['cheese'],
sauce: 'red'
}
} else if (pizzaType.toString().trim() === 'fancy') {
pizza = {
type: 'fancy',
toppings: ['cheese','chicken','arugula'],
sauce: 'pesto'
}
}
return pizza;
}
app.get('/orders', (request, response) => {
var pizzaOrder;
if (typeof request.query.email != "undefined") {
pizzaOrder = cloudCache.getFromCache(request.query.email);
} else {
response.send("Provide an email address to get order details");
}
if(typeof pizzaOrder != "undefined") {
response.json({
customer: request.query.email,
order: pizzaOrder
});
}
response.send("No orders found for " + request.query.email);
})
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'))
})