-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
76 lines (63 loc) · 1.98 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
70
71
72
73
74
75
76
const express = require("express");
const exphbs = require("express-handlebars");
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const session = require("express-session")
const fileUpload = require("express-fileupload");
const mongoStore = require("connect-mongo")(session);
require('dotenv').config({path:"./config/keys.env"});
//const bestsellers = require("./model/bestSellers")
//const category = require("./model/category")
const productModel = require("./model/products")
const app = express();
app.engine('handlebars', exphbs({
helpers: {
iff : function(condition) {
return condition == true;
}
}
}));
app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static("public"));
mongoose.connect(process.env.MONGODB_CONNECTION_KEY, {useNewUrlParser: true, useUnifiedTopology: true})
.then(()=>{
console.log(`Connected to mongo yeay`)
})
.catch(err=>console.log(`Cant connect to mongo due to ${err}`))
const generalController = require("./controller/general");
const productController = require("./controller/product");
const usersController = require("./controller/users");
app.use(session({
secret: `${process.env.SECRET_KEY}`,
resave: false,
saveUninitialized: true,
store: new mongoStore({
mongooseConnection: mongoose.connection
}),
cookie: {
maxAge: 180 * 60 * 1000
}
}))
app.use((req,res,next)=>{
res.locals.user = req.session.userInfo;
res.locals.session = req.session;
next();
})
app.use((req,res,next)=>{
if (req.query.method == "PUT"){
req.method = "PUT";
}
else if (req.query.method == "DELETE"){
req.method = "DELETE";
}
next();
})
app.use(fileUpload());
app.use("/", generalController);
app.use("/products", productController);
app.use("/users", usersController);
const PORT = process.env.PORT;
app.listen(PORT,() => {
console.log("Connected to Asahi's Den");
})