-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (89 loc) · 2.95 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
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
const express = require('express'); // express for creating server, returns a function
const app = express();
const mongoose = require('mongoose'); // ODM library to connect mongodb
const blogRoutes = require('./routes/blog-routes');
const adminRoutes = require('./routes/admin');
const error = require('./controller/error');
const errorRoutes = require('./routes/error');
const auth = require('./routes/auth');
const cors = require('cors');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const cookieParser = require('cookie-parser');
const csurf = require('csurf');
const path = require('path'); // Require the path module
// connect to mongodb & listen for requests
const dbURI = process.env.mongodb;
mongoose.connect(dbURI, { useNewUrlParser: true, useUnifiedTopology: true }) // async task
.then(result => app.listen(3000, () => { // fires callback function
console.log("the server live in http://localhost:3000")
}))
.catch(err => console.log("an error occured while trying to connect db."+err));
// listen for requests on port 3000, default host is localhost
// register view engine, Embedded JavaScript
app.set('view engine', 'ejs');
app.set('views', [
path.join(__dirname, 'views'), // Default view directory
path.join(__dirname, 'views/admin'), // Additional view directory for admin views
]);
// middleware & static files
app.use(express.static('public'));
app.use(express.urlencoded({ extended: false })); // get form details as body, body parser
app.use(express.json()); // Parse incoming JSON requests
app.use(cookieParser());
app.use(cors());
app.use(csurf({ cookie: true })); // Middleware to attach CSRF token to the response locals, Configure CSRF middleware
const store = new MongoDBStore({
uri: process.env.mongodb,
collection: 'sessions'
});
app.use(session({
secret: 'your-secret-key', // Replace with a secure secret key
resave: false,
saveUninitialized: true,
cookie : {
maxAge: 3600000,
},
store: store
}));
// Routes
app.get('/', (req, res) => {
res.redirect('/blogs');
});
app.get('/about', (req, res) => {
res.render('about', { title: 'About',isAuth : req.session.isAuth} );
});
app.use('/user', auth);
app.use('/blogs', blogRoutes);
app.use('/admin', adminRoutes);
app.use(error.errorPage);
/* app.get('/blogs', (req, res) => {
Blog.find().sort({ createdAt: -1 })
.then(result => {
res.render('index', { blogs: result, title: 'All blogs' });
})
.catch(err => {
console.log(err);
});
}); */
/* app.get('/all-blogs', (req, res) => {
Blog.find()
.then(result => {
res.send(result);
})
.catch(err => {
console.log(err);
});
}); */
/*
app.delete('/blogs/:id', (req, res) => { // that doesn't work
const id = req.params.id;
Blog.findByIdAndDelete(id)
.then(result => {
res.json({ redirect: '/blogs' });
})
.catch(err => {
console.log(err);
});
}); */
// 404 page