-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
97 lines (75 loc) · 2.42 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
var express = require('express');
global.app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('cookie-session');
var flash = require('connect-flash');
var userPeers = require('./userPeers');
// view engine setup
app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static(require('path').join(__dirname, '/public')));
app.use(express.static(__dirname + '/bower_components'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(session({
key: 'sessionId',
secret: 'session_cookie_secret+sdjfiawe958723',
cookie: {
maxAge: 1000 * 60
}
}));
app.use(flash());
var server = require('http').createServer(app);
var expresspeerserver = ExpressPeerServer(server, {
debug: true
});
expresspeerserver.on('connection', function (id) {
userPeers.add(id);
console.log('new connection, id is : %s', id);
userPeers.show();
});
expresspeerserver.on('disconnect', function (id) {
userPeers.remove(id);
console.log('peer disconnect, : %s', id);
userPeers.show();
});
app.use('/myapp', expresspeerserver);
/*
* api
*/
var controllers = require('./controllers');
app.get('/', controllers.root.home);
app.get('/index', controllers.root.home);
app.get('/start', controllers.root.start);
app.get('/scheduling', controllers.root.scheduling)
// 註冊
app.post('/users', controllers.users.create);
// 登入
app.post('/login', controllers.users.login);
// 登出
app.get('/logout', controllers.users.logout);
// 檢舉
app.post('/reports', controllers.users.report);
// 人氣列表
app.get('/hots', controllers.users.hot);
app.get('/userPeer', controllers.userPeers.getPeer);
app.get('/callOn/:peerId', controllers.userPeers.callOn);
app.get('/callOff/:peerId', controllers.userPeers.callOff);
// 喜歡或不喜歡
app.post('/likes', controllers.likes.create);
// app.all('/flash', function(req, res){
// req.flash('test', 'it worked');
// res.redirect('/test')
// });
// app.all('/test', function(req, res){
// res.send(JSON.stringify(req.flash('test')));
// });
server.listen(process.env.PORT || 9000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});