forked from robrighter/node-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (81 loc) · 2.56 KB
/
server.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
//setup Dependencies
require(__dirname + "/lib/setup").ext( __dirname + "/lib").ext( __dirname + "/lib/express/support");
var connect = require('connect')
, express = require('express')
, sys = require('sys')
, io = require('Socket.IO-node')
, port = (process.env.PORT || 8081);
//Setup Express
var server = express.createServer();
server.configure(function(){
server.set('views', __dirname + '/views');
server.use(connect.bodyDecoder());
server.use(connect.staticProvider(__dirname + '/static'));
server.use(server.router);
});
//setup the errors
server.error(function(err, req, res, next){
if (err instanceof NotFound) {
res.render('404.ejs', { locals: {
header: '#Header#'
,footer: '#Footer#'
,title : '404 - Not Found'
,description: ''
,author: ''
,analyticssiteid: 'XXXXXXX'
},status: 404 });
} else {
res.render('500.ejs', { locals: {
header: '#Header#'
,footer: '#Footer#'
,title : 'The Server Encountered an Error'
,description: ''
,author: ''
,analyticssiteid: 'XXXXXXX'
,error: err
},status: 500 });
}
});
server.listen( port);
//Setup Socket.IO
var io = io.listen(server);
io.on('connection', function(client){
console.log('Client Connected');
client.on('message', function(message){
client.broadcast(message);
client.send(message);
});
client.on('disconnect', function(){
console.log('Client Disconnected.');
});
});
///////////////////////////////////////////
// Routes //
///////////////////////////////////////////
/////// ADD ALL YOUR ROUTES HERE /////////
server.get('/', function(req,res){
res.render('index.ejs', {
locals : {
header: '#Header#'
,footer: '#Footer#'
,title : 'Page Title'
,description: 'Page Description'
,author: 'Your Name'
,analyticssiteid: 'XXXXXXX'
}
});
});
//A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function(req, res){
throw new Error('This is a 500 Error');
});
//The 404 Route (ALWAYS Keep this as the last route)
server.get('/*', function(req, res){
throw new NotFound;
});
function NotFound(msg){
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
console.log('Listening on http://0.0.0.0:' + port );