-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
90 lines (84 loc) · 3.29 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
const express = require('express');
const app = express();
// middleware
const flash = require('connect-flash');
const moment = require('moment');
const engine = require('ejs-locals');
const session = require('express-session');
const multer = require('multer');
const upload = multer();
const { check } = require('express-validator');
const port = process.env.PORT || 5001;
// include controller
const userController = require('./controllers/user');
const articleController = require('./controllers/articles');
const categoryController = require('./controllers/categories');
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(flash());
app.use(session({
secret: 'somevalue',
resave: false,
saveUninitialized: true
}));
// use built-in middleware static() to serve static files
app.use(express.static(__dirname + '/public'));
app.engine('ejs', engine);
app.set('views', './views');
app.set('view engine', 'ejs');
// 格式化時間
app.locals.moment = moment;
const redirectBack = (req, res) => {
res.redirect('back');
}
const checkLogin = (req, res, next) => {
if (!req.session.username) return res.redirect('/login');
next();
}
// router
app.get('/login', userController.loginPage);
app.post('/login', userController.handleLogin, redirectBack);
app.get('/logout', userController.handleLogout);
app.get('/about', (req, res) => res.render('aboutPage'));
app.get('/', articleController.getAllArticles, redirectBack);
app.get('/indexArticle/:id', (req, res) => res.set('Access-Control-Allow-Origin', '*'));
app.get('/indexArticle/:id', articleController.getOneArticle, redirectBack);
app.get('/list', articleController.getAllArticlesList, redirectBack);
app.get('/category', categoryController.getAllCategories, redirectBack);
app.get('/backstage', checkLogin, articleController.backstage);
app.get('/createArticle', checkLogin, articleController.createArticlePage, redirectBack);
app.post('/createArticle', checkLogin, upload.single('image'), [
check('title').notEmpty().withMessage('請輸入文章標題'),
check('content').notEmpty().withMessage('請輸入文章內容'),
check('category').custom((value, { req }) => {
if (value === 'not-selected') {
throw new Error('請選擇文章分類')
}
return true;
})
],
articleController.checkErr,
articleController.handleUploadImage,
articleController.handleCreateArticle,
redirectBack);
app.get('/deleteArticle/:id', checkLogin, articleController.handleDelete, redirectBack);
app.get('/updateArticle/:id', checkLogin, articleController.updateArticlePage)
app.post('/updateArticle/:id', checkLogin, upload.single('image'), [
check('title').notEmpty().withMessage('請輸入文章標題'),
check('content').notEmpty().withMessage('請輸入文章內容'),
check('category').custom((value, { req }) => {
if (value === 'not-selected') {
throw new Error('請選擇文章分類')
}
return true;
})
],
articleController.checkErr,
articleController.handleUploadImage,
articleController.handleUpdateArticle,
redirectBack);
app.get('/createCategory', checkLogin, categoryController.createCategoryPage, redirectBack)
app.post('/createCategory', checkLogin, categoryController.handleCreateCategory, redirectBack);
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});