-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (89 loc) · 2.74 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
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
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')
require('dotenv').config({path:"./config/keys.env"});
const { capitalizeFirst, formatCurrency } = require('./jr-node-utils');
//set PORT here
const PORT = process.env.PORT;
// Init express
const app = express();
// Set up static routes
app.use(express.static('public'))
//CREATE HANDLEBARS AND CUSTOM HELPERS
const hbs = exphbs.create({
helpers:{
capitalizeFirst: (word) => capitalizeFirst(word),
toCurrency : (value) => formatCurrency(value),
ifeq : (v1, v2, options)=> v1 == v2 ? options.fn(this) : options.inverse(this),
toJSON : (obj) => JSON.stringify(obj),
getFirstName : (str) => str.split(' ')[0],
}
})
//Set template engine
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// Custom middlewares for PUT and DELETE from forms
app.use((req,res,next)=>{
console.log('custom put delete middleware')
if(req.query.method=="PUT")
{
console.log('custom put delete middleware PUT')
req.method="PUT"
}
else if(req.query.method=="DELETE")
{
req.method="DELETE"
}
next();
})
app.use(fileUpload({
debug: true
}));
// Setting up session
app.use(session({
secret: `${process.env.SECRET_SESSION}`,
resave: false,
saveUninitialized: true
}))
app.use((req,res,next)=>{
res.locals.user= req.session.userInfo;
next();
})
// Connect to Mongo Atlas Cluster
mongoose.connect(process.env.MONGODB_STRING, {useNewUrlParser: true, useUnifiedTopology: true})
.then(()=> console.log('DataBase connected.'))
.catch( err => console.log('Error connecting to DataBase: ' + err))
// load controllers
const generalController = require('./controllers/general')
const productsController = require('./controllers/products')
const userController = require('./controllers/user')
// maping controllers
app.use('/',generalController)
app.use('/products/',productsController)
app.use('/user',userController)
// app.engine.registerHelper("noop", function(options) {
// return options.fn(this);
// });
// Pop-ups
// Customer Registration
// app.get('/registration', (req,res) =>{
// const expressions = {
// title: 'User Registration'
// }
// res.render('registration',expressions)
// });
// // Login
// app.get('/login', (req,res) =>{
// const expressions = {
// title: 'User Login'
// }
// res.render('login',expressions)
// });
app.listen(PORT, ()=>{
console.log(`Server running on PORT: ${PORT}`)
})