forked from mayajuni/johayoBlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
123 lines (107 loc) · 3.87 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var http = require('http');
var session = require('express-session');
var RedisStore = require ('connect-redis')(session);
var domain = require('express-domain-middleware');
var validator = require('validator');
var colors = require('colors');
/* config */
var config = require('./config/config');
/* util */
var tg = require('./util/tg');
var loginUtil = require('./util/loginUtil');
/* routes */
var menu = require('./routes/menu');
var login = require('./routes/login');
var board = require('./routes/board');
var comment = require('./routes/comment');
var file = require('./routes/file');
var bookmark = require('./routes/bookmark');
var adminBoard = require('./routes/adminBoard');
var adminMenu = require('./routes/adminMenu');
var msg = require('./routes/msg');
var app = express();
// uncomment after placing your favicon in /public
app.use(favicon(__dirname + '/public/imgs/favicon.ico'));
app.use(logger('dev'));
app.use(domain);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
store: new RedisStore({
port: config.redis.port,
host: config.redis.host,
pass: config.redis.password,
ttl: config.redis.ttl
}),
name : config.session.name,
secret: config.session.secret,
proxy: true,
resave : false,
saveUninitialized : true,
cookie: {
secure: false
}
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join('/johayo/upload')));
app.get('/download', function(req, res){
var path = validator.isNull(req.param('path')) ? error.throw(409,'Please check path.') : req.param('path');
var name = validator.isNull(req.param('name')) ? '' : req.param('name');
res.download(path,name);
});
/* 로그인이 필요한 서비스 */
app.use('/api/file', loginUtil.check);
app.use('/api/bookmark', loginUtil.check);
app.use('/api/adminMenu', loginUtil.check);
app.use('/api/adminBoard', loginUtil.check);
/* 없는 서비스 */
app.use('/api/menu', menu);
app.use('/api/login', login);
app.use('/api/board', board);
app.use('/api/comment', comment);
app.use('/api/file', file);
app.use('/api/msg', msg);
app.use('/api/bookmark', bookmark);
app.use('/api/adminMenu', adminMenu);
app.use('/api/adminBoard', adminBoard);
/* 일단 get으로 요청된 것들은 바로 index.html으로 보여준다. */
app.get('/', function(req, res) {
/*res.sendFile(__dirname + '/public/html/comming.html');*/
res.sendFile(__dirname + '/public/html/index.html');
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
throw err;
});
// catch all error handler
app.use(function errorHandler(err, req, res, next) {
/* 에러 처리 */
err.status = validator.isNull(err.status) ? 500 : err.status;
/* 텔레그램으로 나한테 오류 메세지 전송 */
if(err.status != 401 && err.status != 409 && err.status != 4019 && err.status != 404){
var tgData = {};
tgData.ip = req.headers['x-forwarded-for'] || req.ip;
tgData.url = req.url;
tgData.division = 'error';
tgData.content = err.message + ' ['+tgData.url + '], ['+tgData.ip+']';
tg.sendMsg(tgData);
}
console.log('error on request %s | %s | %d'.red, req.method, req.url, err.status);
console.log(err.stack.red);
err.message = err.status == 500 ? 'Something bad happened. :(' : err.message;
res.status(err.status).send(err.message);
});
// error handlers
http.createServer(app).listen(config.web.port, function(){
console.log('Express server listening on port ' + config.web.port);
});
module.exports = app;