-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
144 lines (115 loc) · 3.07 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, Master = require('./master');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret : 'secret key'}));
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Exception Handler - js 에러 시 서버가 죽는 현상 방지
process.on('uncaughtException', function(err) {
// TODO : err.stack 사용 필요
// http://nodeqa.com/nodejs_ref/5 참조
console.log('Caught Exception : ' + err);
});
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
(function action() {
app.get('/', function(req, res){
res.render('login');
});
app.get('/users', user.list);
app.post('/waitingRoom', function(req, res) {
var sName = req.body.name;
if (!sName || sName.trim() === '') {
res.render('login', {
sErrorMsg : 'Input your name'
});
return;
}
if (Master.hasUser(sName)) {
res.render('login', {
sErrorMsg : 'Nickname is already exists'
});
return;
}
var htUser = Master.createUser(sName);
Master.addUser(htUser);
req.session.sName = sName;
res.render('waitingroom', {
user : htUser
, aRoomList : Master.getRoomList()
});
});
app.post('/randomMatch', function(req, res) {
if (req.session.sName == 'undefiend') {
res.render('login');
return;
}
var htRoom = Master.getAvailableRoom()
, htUser = Master.getUserByName(req.session.sName);;
if (!htRoom) {
// TODO : show error
return;
}
res.render('index', {
htRoom : htRoom
, htUser : htUser
});
});
app.post('/makeRoom', function(req, res) {
if (req.session.sName == 'undefiend') {
res.render('login');
return;
}
var sRoomName = req.body.roomName
, htRoom = Master.createRoom(sRoomName)
, htUser = Master.getUserByName(req.session.sName);
// TODO : if sRoomName is blank, its process have to doing by ajax call
// TODO : if sRoomName is dupicated, its process have to doing by ajax call
Master.addRoom(htRoom);
res.render('index', {
htRoom : htRoom
, htUser : htUser
});
});
app.get('/join/:roomId', function(req, res) {
if (req.session.sName == 'undefiend') {
res.render('login');
return;
}
var sRoomId = req.params.roomId
, htUser = Master.getUserByName(req.session.sName)
, htRoom = Master.getRoomById(sRoomId);
if (!htRoom) {
return;
}
res.render('index', {
htRoom : htRoom
, htUser : htUser
});
});
}())
var server = http.createServer(app);
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
require('./socket')(server);