Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
161 lines (131 loc) · 4.47 KB
/
Copy pathserver.js
File metadata and controls
161 lines (131 loc) · 4.47 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"use strict";
require('dotenv').config();
const PORT = process.env.PORT || 8080;
const ENV = process.env.ENV || "development";
const express = require("express");
const bodyParser = require("body-parser");
const sseExpress = require('sse-express');
const sass = require("node-sass-middleware");
const app = express();
const knexConfig = require("./knexfile");
const knex = require("knex")(knexConfig[ENV]);
const morgan = require('morgan');
const knexLogger = require('knex-logger');
// Seperated Routes for each Resource
const usersRoutes = require("./routes/users");
const foodRoutes = require("./routes/food");
const order = require('./lib/order-helpers');
const locationRoutes = require("./routes/location");
const cookieSession = require('cookie-session');
app.use(cookieSession({
name: 'session',
keys: ['hgiehgiehiwheihge', 'eutiewginibebi']
}));
// Load the logger first so all (static) HTTP requests are logged to STDOUT
// 'dev' = Concise output colored by response status for development use.
// The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
app.use(morgan('dev'));
// app.use(sseExpress());
// Log knex SQL queries to STDOUT as well
app.use(knexLogger(knex));
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/styles", sass({
src: __dirname + "/styles",
dest: __dirname + "/public/styles",
debug: true,
outputStyle: 'expanded'
}));
app.use(express.static("public"));
// Mount all resource routes
app.use("/api/users", usersRoutes(knex));
app.use("/api/food", foodRoutes(knex));
app.use("/api/location", locationRoutes(knex));
// Twilio
// const accountSid = process.env.TWILIO_SID;
// const authToken = process.env.TWILIO_AUTH;
// const client = require('twilio')(accountSid, authToken);
// Home page
app.get("/", (req, res) => {
res.redirect("/locations");
});
app.get("/locations", (req, res) => {
res.render("index", {currUser: req.session.user});
});
app.get("/myaccount", (req, res) => {
res.render("myaccount", {currUser: req.session.user});
});
app.get("/locations/:id", (req, res) => {
let location_id = req.params.id;
let templateVars = {location_id: location_id, currUser: req.session.user};
res.render("menu", templateVars);
});
app.get("/checkout/:id", (req, res) => {
res.render("checkout", {currUser: req.session.user, location_id: req.params.id});
});
// retrieve contents of a message sent back by restaurant, update order status
app.post('/sms', (req, res) => {
res.writeHead(200, {'Content-Type': 'text/xml'});
const time = req.body['Body'];
order.update(1, time);
res.end();
});
// processing state for order; not yet confirmed but restaurant notified
app.post("/checkout/process", (req, res) => {
const cart = JSON.parse(req.body.cart);
const clientEmail = req.body.clientEmail;
let body = `New order from ${clientEmail}:\n`;
for (let item of cart) {
body += `${item.name}, quantity: ${item.quantity}\n`
}
client.messages
.create({
body: body,
from: '+16474944728',
to: '+12047208938'
})
.then(message => console.log(message.sid))
.done();
})
// used to check the status of an order
app.get('/check-status', sseExpress(), function(req, res) {
req.socket.setKeepAlive();
function check() {
if (order.status === 1) {
res.sse({
event: 'received',
data: { welcomeMsg: 'Order confirmed' }
});
order.update(0);
return;
}
setTimeout(check, 5000);
}
check();
});
// page user is brought to while waiting for order to be confirmed
app.get('/order/purgatory/:clientPhone', (req, res) => {
res.render('order-purgatory', {clientPhone: req.params.clientPhone,
currUser: req.session.user});
});
// page user is brought to when the order is confiremdd
app.get('/order-confirmed/:clientPhone', (req, res) => {
const clientPhone = req.params.clientPhone;
const body = `Order confirmed. Your doughnuts will be ready in ${order.time} minutes!`;
client.messages
.create({
body: body,
from: '+16474944728',
to: clientPhone
})
.then(message => console.log(message.sid))
.done();
res.render('order-confirmed', {currUser: req.session.user});
})
app.get('/logout', (req, res) => {
req.session = null;
res.redirect('locations');
});
app.listen(PORT, () => {
console.log("Example app listening on port " + PORT);
});