-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (53 loc) · 1.91 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
57
58
59
60
61
62
63
64
65
66
67
68
const session = require('express-session');
const express = require("express");
const app = express();
const port = 3000;
const restaurantRouter = require("./routes/restaurant_routes");
const adminRouter = require("./routes/admin_routes");
const bistrosyncAdminRouter = require("./routes/bistrosync_admin_routes");
const Company = require("./src/models/company");
const mongoose = require('mongoose');
const url = "mongodb+srv://mparry:[email protected]/?retryWrites=true&w=majority&appName=BistroSync";
mongoose.connect(url);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
db.once('open', function (callback) {
console.log("Database Connection succeeded");
})
app.use(express.static("src"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(session({ secret: 'Secret_Key', resave: false, saveUninitialized: false }));
app.use((err, req, res, next) => {
console.log("ERROR: " + err.message);
res.status(500).send("Error: " + err.message);
});
app.get("/", (req, res) => {
res.redirect("/test");
});
app.use("/:restaurant", async (req, res, next) => {
// Check if restaurant exists
const restaurant = req.params.restaurant;
const company = await Company.findOne({ name: restaurant });
if (!company) {
res.status(404).send("Restaurant not found");
return;
}
req.company = company._id;
return next();
}, restaurantRouter);
app.use("/:restaurant/admin", (req, res, next) => {
if (req.path !== '/login') {
const restaurant = req.company;
return !req.session.user_id ? res.redirect('login') : next();
} else {
next();
}
}, adminRouter);
app.use("/bistrosync/admin", bistrosyncAdminRouter);
if (process.env.NODE_ENV !== 'test') {
app.listen(port, () => {
console.log("Started server");
});
}
module.exports = app;