-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
95 lines (74 loc) · 2.55 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
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
const express = require('express');
const exphbs = require('express-handlebars');
const app = express();
//const fs = require('fs');
const session = require('client-sessions');
const isLoggedIn = require("../assignment1/middleware/authentication");
const isAdmin =require("../assignment1/middleware/admin");
require('dotenv').config({path:"./config/keys.env"})
var bodyParser = require('body-parser')
let mongoose = require('mongoose');
let password = encodeURIComponent(process.env.xxx);
mongoose.connect(`mongodb+srv://swiggerCompany:${password}@cluster0.w3igo.mongodb.net/swiggerCompany?retryWrites=true&w=majority`,{ useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
const path = require('path');
const meal = require('./model/meal');
// app.engine('handlebars', exphbs({ defaultLayout: 'main'}));
// app.set('view engine', 'handlebars');
app.set("views", "./views");
app.engine('.hbs', exphbs({extname: ".hbs"}));
app.set('view engine', '.hbs');
app.use(express.static(path.join(__dirname,'/public')));
app.use(session({
cookieName: "session",
secret: "swigger_login",
//duration: 2*60*1000,
//activeDuration: 60*1000
}))
// app.use((req, res, next) =>{
// req.session.userInfo = {};
// })
const login = require('./controller/#.js');
const register = require('./controller/register.js');
const meals = require('./controller/meal.js');
const mealModel = require('./model/meal');
const { title } = require('process');
session.userInfo = null;
app.get('/', (req, res) =>{
// console.log('currently on hime page');
// req.session.userInfo = null;
mealModel.find({})
.then((item)=>{
const filteredProducts = item.map(product=>{
return {
id: product._id,
title: product.title,
price: product.price,
desc: product.desc,
imgAddress: product.imgAddress
}
});
res.render("home", {
title: "SWIGGERS",
style: 'home.css',
data: filteredProducts
});
});
})
app.get('/dashboard', isLoggedIn, isAdmin, (req, res) => {
console.log("here");
res.render('dashboard', {
title: "dashboard page"
})
})
app.get('/admindashboard', isLoggedIn, (req, res) => {
console.log("here");
res.render('admindashboard', {
title: "admindashboard page"
})
})
app.use('/', login, register, meals);
app.listen(process.env.PORT||3000, () => {
console.log('this server is ruuning on port 3000');
});