-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (75 loc) · 2.25 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
let express = require('express');
let app = express();
let mongoose = require('mongoose');
let multer = require('multer');
let cookieParser = require('cookie-parser');
require('dotenv').config();
let Post = require("./models/post.model").Post;
let postsRouter = require('./routes/posts.route');
let callbackRouter = require('./routes/callback-requests.route');
let emailsRouter = require('./routes/emails.route');
let usersRouter = require('./routes/users.route');
let auth = require('./controllers/auth.js');
// Set EJS veiw engine
app.set('view engine', 'ejs');
// Connect to database
mongoose.connect(process.env.DB, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Middleware
app.use(express.static('public'));
app.use(express.json());
app.use(cookieParser());
let fileStorage = multer.diskStorage({
destination: (req, file, cb) => cb(null, 'public/images'),
filename: (req, file, cb) => cb(null, file.originalname)
})
app.use(multer({storage: fileStorage}).single('imageFile'));
// posts routes
app.use('/posts', postsRouter);
// callback routes
app.use('/callback-requests', callbackRouter);
// email routes
app.use('/emails', emailsRouter);
// users routes
app.use('/users', usersRouter)
// Landmark route
app.get('/landmark', async (req, res) => {
let id = req.query.id;
let post = await Post.findOne({id: id});
res.render('landmark', {
title: post.title,
imageURL: post.imageURL,
date: post.date,
country: post.country,
text: post.text
});
});
// admin/user routes
app.get('/admin', (req, res) => {
let token = req.cookies['auth_token'];
if(token !== null && auth.checkToken(token)){
res.render('admin');
} else {
res.redirect('/login');
}
});
app.get('/login', (req, res) => {
let token = req.cookies['auth_token'];
if(token !== null && auth.checkToken(token)){
res.redirect('/admin');
} else {
res.render('login');
}
})
// get .env routes
app.get('/apikey', (req, res) => {
res.send(process.env.APIKEY);
})
app.get('/secret', (req, res) => {
res.send(process.env.SECRET);
})
// Load sever on port 3000
let port = process.env.PORT || 3000
app.listen(port, () => console.log(`Listening on port ${port}.....`));