forked from lizlux/ChatApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
103 lines (92 loc) · 2.03 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
/*
* Module Dependencies
*/
var express = require( 'express' ),
stylus = require( 'stylus' ),
nib = require( 'nib' ),
_ = require( 'underscore' ),
app = express(),
http = require( 'http' ),
server = http.createServer( app ).listen( 3000 ),
io = require( 'socket.io' ).listen( server ),
requirejs = require( 'requirejs' ),
pages = [
{
template: 'index',
title: 'Home',
path: ''
},
{
title: 'Leap',
template: 'leap'
},
{
title: 'Leap Gestures',
template: 'gestures'
},
{
title: 'scm',
template: 'SCM Music Player Sandbox'
},
{
title: 'Ajax Tabs Example',
template: 'ajax_tabs'
},
{
title: 'Testing WhiteSpace NoWrap in IE',
template: 'nowrap'
},
{
title: 'Playing with bourbon',
template: 'bourbon'
},
{
title: 'Three.js',
template: 'threejs'
}
];
/**
* Route all pages
* Requires data.path and data.title
* @param data object
*/
function routeApp( data ) {
if( data.path === undefined ) {
data.path = data.template;
if( data.path === undefined ) {
throw "A path or template must be specified for each page route.";
}
}
app.get( '/' + data.path, function( req, res ) {
res.render( data.template, {
title: data.title
} );
} );
}
_.each( pages, routeApp );
app.set( 'views', __dirname + '/views' );
app.set( 'view engine', 'jade' );
app.use( express.logger( 'dev' ) );
app.use( stylus.middleware( {
src: __dirname + '/public',
compile: function( str, path ) {
return stylus( str )
.set( 'filename', path )
.use( nib() );
}
} ) );
app.use( requirejs );
// set public files directory
app.use( express.static( __dirname + '/public' ) );
io.sockets.on( 'connection', function( socket ) {
socket.on( 'setPseudo', function( data ) {
socket.set( 'pseudo', data );
} );
socket.on( 'message', function( message ) {
socket.get( 'pseudo', function( error, name ) {
var data = { 'message' : message, pseudo : name };
socket.broadcast.emit( 'message', data );
console.log( 'user ' + name + ' sent this : ' + message );
} );
} );
} );