Created
December 4, 2013 15:44
-
-
Save xinranxiao/7789781 to your computer and use it in GitHub Desktop.
Boilerplate Express Node.js App with Socket.io.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var express = require('express'); | |
var routes = require('./routes'); | |
var user = require('./routes/user'); | |
var http = require('http'); | |
var path = require('path'); | |
var app = express(); | |
// all environments | |
app.set('port', process.env.PORT || 3000); | |
app.set('views', path.join(__dirname, 'views')); | |
app.set('view engine', 'ejs'); | |
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'))); | |
var server = http.createServer(app); | |
var io = require('socket.io').listen(55550); | |
io.set('log level', 1); | |
io.sockets.on('connection', function (socket) { | |
socket.emit('news', { hello: 'world' }); | |
socket.on('my other event', function (data) { | |
console.log(data); | |
}); | |
}); | |
app.get('/', routes.index); | |
server.listen(app.get('port'), function(){ | |
console.log('Express server listening on port ' + app.get('port')); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= title %></title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<h1><%= title %></h1> | |
<p>Welcome to <%= title %></p> | |
<p id="gamesList"></p> | |
<script src="http://localhost:55550/socket.io/socket.io.js"></script> | |
<script> | |
window.onload = function() { | |
var socket = io.connect('http://localhost:55550'); | |
socket.on('news', function (data) { | |
console.log(data); | |
document.querySelector('#gamesList').innerHTML = data; | |
}); | |
}; | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment