forked from sakshammishra5/e-commerce-shop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (62 loc) · 2.13 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
const path = require('path');
const express = require('express');
const app = express();
const PORT = 4444;
const MongoStore = require('connect-mongo');
const mongoose = require('mongoose');
const session = require('express-session');
const flash = require('connect-flash');
const passport = require('./auth/passport');
const hbs = require('hbs');
const multer = require('multer');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
console.log(path.extname(file.originalname));
cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
}
})
const upload = multer({ });
app.use(upload.single('image'));
app.use(flash());
app.set('view engine', 'hbs');
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'public')));
require('dotenv').config()
hbs.registerPartials(__dirname + '/views/partials');
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URL })
}))
app.use(passport.initialize());
app.use(passport.session());
app.get('/', (req, res, next) => {
if (req.user) return res.redirect('/shop/profile');
return res.redirect('/login');
})
app.get('/logout', function (req, res, next) {
req.logout(function (err) {
if (err) { return next(err); }
res.redirect('/');
});
});
const ifNotLoggedIn = require('./middlewares/checkedLoggedIn');
app.use('/signup', ifNotLoggedIn, require('./routes/signup'));
app.use('/login', ifNotLoggedIn, require('./routes/login'));
const isLoggedIn = require('./middlewares/isLoggedIn');
app.use('/admin', isLoggedIn, require('./routes/admin'));
app.use('/shop', isLoggedIn, require('./routes/shop'));
mongoose.connect(process.env.MONGO_URL)
.then(() => {
app.listen(PORT, () => {
console.log(`http://localhost:` + PORT);
});
})
.catch((err) => {
console.log(err);
})