Created
February 11, 2014 04:12
-
-
Save iDVB/8929149 to your computer and use it in GitHub Desktop.
Rendr + Socket.io base Express file
This file contains 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
var express = require('express') | |
, http = require('http') | |
, rendr = require('rendr') | |
, config = require('config') | |
, lampAPI = require('./api/index') | |
, io = require('socket.io') | |
, ioServer | |
, webSocket | |
, connectedUsers = {} | |
, app = express(); | |
/** | |
* Initialize Express middleware stack. | |
*/ | |
app.use(express.compress()); | |
app.use(express.static(__dirname + '/public')); | |
app.use(express.logger()); | |
app.use(express.bodyParser()); | |
/** | |
* Initialize our Rendr server. | |
*/ | |
var rendrApp = rendr.createServer({ | |
dataAdapterConfig: config.api, | |
appData: config.appData | |
}); | |
ioServer = http.createServer(app); | |
/** | |
* To mount Rendr, which owns its own Express instance for better encapsulation, | |
* simply add `server` as a middleware onto your Express app. | |
* This will add all of the routes defined in your `app/routes.js`. | |
* If you want to mount your Rendr app onto a path, you can do something like: | |
* | |
* app.use('/my_cool_app', server); | |
*/ | |
app.use('/api/v1', lampAPI()); | |
app.use(rendrApp); | |
/** | |
* Start the Express server. | |
*/ | |
function start(){ | |
var port = process.env.PORT || config.server.port; | |
//app.listen(port); | |
webSocket = io.listen(ioServer); | |
ioServer.listen(port); | |
webSocket.sockets.on('connection', function (socket) { | |
console.log('connected client'); | |
}); | |
console.log("server pid %s listening on port %s in %s mode", | |
process.pid, | |
port, | |
app.get('env') | |
); | |
} | |
/** | |
* Only start server if this script is executed, not if it's require()'d. | |
* This makes it easier to run integration tests on ephemeral ports. | |
*/ | |
if (require.main === module) { | |
start(); | |
} | |
exports.app = app; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment