-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
56 lines (48 loc) · 1.5 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
const express = require('express')
const expressLayouts = require('express-ejs-layouts')
const app = express();
const env = require('dotenv')
const path = require('path')
const routes = require('./server/routes/recipeRoutes');
const fileUpload = require('express-fileupload')
const session = require('express-session')
const cookieParser = require('cookie-parser')
const flash = require('connect-flash');
const mongoose = require('mongoose');
const port = process.env.PORT || 4001
env.config()
app.use(express.urlencoded({extended:true}))
app.use(express.static('public')) //img/image.jpg
app.use(expressLayouts)
app.use(cookieParser('CookingBlogSecure'));
app.use(session({
secret: process.env.SESSION_SECRET,
saveUninitialized: true,
resave: true
}));
app.use(function(req, res, next) {
res.locals.userId = req.session.userId;
res.locals.userName = req.session.username;
next();
});
app.use(flash());
app.use(fileUpload());
app.set('layout','./layouts/main')
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine','ejs')
app.use('/',routes)
require('./server/models/Category');
require('./server/models/Recipe');
require('./server/models/User')
app.listen(port,()=>{
console.log(`Server running on port ${port}`);
mongoose.connect(`${process.env.MONGOURI}`, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('Error connecting to MongoDB:', err);
});
})