-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
102 lines (85 loc) · 1.83 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
/*!
* yellowtail - app.js
* Copyright(c) 2013
* Author: karen <karenpenglabs@gmail.com>
*/
'use strict';
/**
* Module dependencies.
*/
require('response-patch');
var http = require('http');
var path = require('path');
var connect = require('connect');
var render = require('connect-render');
var urlrouter = require('urlrouter');
//socket.io
var SocketIO = require('socket.io');
var sioRoutes = require('./sio_routes');
var config = require('./config');
var routes = require('./routes');
var PUBLIC_DIR = path.join(__dirname, 'public');
var app = connect();
app.use('/public', connect.static(PUBLIC_DIR));
app.use(function (req, res, next) {
res.req = req;
next();
});
//parse http get params
app.use(connect.query());
//parse http post body
app.use(connect.urlencoded());
app.use(connect.json({
strict: true
}));
//parse cookie
app.use(connect.cookieParser());
//session
app.use(connect.session({
key: config.sessionCookie,
secret: config.sessionSecret,
cookie: {
path: '/',
httpOnly: true
},
}));
//handle csrf, do not open it when
if (process.env.NODE_ENV !== 'test') {
app.use(connect.csrf());
}
/**
* res.render helpers
*/
var helpers = {
config: config,
csrf: function (req) {
return req.csrfToken ? req.csrfToken() : '';
}
};
app.use(render({
root: path.join(__dirname, 'views'),
viewExt: '.html',
layout: 'layout',
cache: config.viewCache,
helpers: helpers
}));
/**
* Web site URL routing
*/
app.use(urlrouter(routes));
/**
* Error handler
*/
app.use(function (err, req, res, next) {
err.url = err.url || req.url;
console.error(err);
console.error(err.stack);
if (config.debug) {
return next(err);
}
res.statusCode = 500;
res.send('Server has some problems. :(');
});
var server = module.exports = http.createServer(app);
var sio = SocketIO.listen(server);
sioRoutes(sio);