-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (66 loc) · 1.96 KB
/
app.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
const express = require('express');
const expressLayouts = require('express-ejs-layouts');
require('dotenv').config();
const morgan = require('morgan');
const connectDB = require('./server/models/db');
const session = require('express-session');
const flash = require('connect-flash');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const MongoStore = require('connect-mongo')(session);
const { default: mongoose } = require('mongoose');
const process = require('process');
// dotenv.config( {path: './config/config.env'} );
connectDB();
const app = express();
// static files
app.use(express.urlencoded( { extended: true } ));
app.use(express.static('public'));
app.use('/css', express.static(__dirname + 'public/css'));
// set templating engine
app.use(expressLayouts);
app.set('layout', './layouts/main');
app.set('view engine', 'ejs');
// session
app.use(session({
secret: 'aiaShoppingCart',
resave: false,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: mongoose.connection
}),
cookie: { maxAge: 180 * 60 * 1000 }
}))
// flash messages middleware
app.use((req, res, next) => {
res.locals.message = req.session.message;
delete req.session.message;
next();
})
// routes
const routes = require('./server/routes/shopRoutes.js');
app.use('/', routes);
app.use('/checkout', routes);
app.use('/add-to-cart', routes);
app.use('/remove', routes);
app.use('/remove-all', routes);
app.use('/buy', routes);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));
app.use(cookieParser());
app.use(flash());
app.use(function(req, res, next){
res.locals.session = req.session;
next();
})
// logging
if (process.env.NODE_ENV === 'development') {
app.use(morgan('dev'))
}
// listen on port 3000
const port = process.env.PORT || 8080;
app.listen(
port, () => {
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${port}`)
}
);