Permalink
Browse files

Expose server before startup

  • Loading branch information...
1 parent b6d8042 commit f6b258f28750e9c56bc8d726b19562f84419f665 @camshaft committed Feb 7, 2013
Showing with 60 additions and 5 deletions.
  1. +50 −0 README.md
  2. +9 −4 lib/runner.js
  3. +1 −1 package.json
View
@@ -63,6 +63,56 @@ startup start
and startup will bind to the `PORT` environment variable.
+### Sockjs Example
+
+To install any handlers needed, sockjs for example, listen for the `ready` event:
+
+```js
+var express = require("express")
+ , sockjs = require("sockjs");
+
+// Export the express app
+var app = module.exports = express();
+
+// Setup the sockjs server
+var echo = sockjs.createServer();
+echo.on('connection', function(conn) {
+ conn.on('data', function(message) {
+ conn.write(message);
+ });
+ conn.on('close', function() {});
+});
+
+app.get("/", function (req, res){
+ res.send("Hello!");
+});
+
+// Install the handlers
+app.on("ready", function(server){
+ echo.installHandlers(server, {prefix: "/echo"});
+});
+```
+
+### Listening Callback
+
+To know when the server is listening on a port, bind to the `listening` event:
+
+```js
+var express = require("express");
+
+// Export the express app
+var app = module.exports = express();
+
+app.get("/", function (req, res){
+ res.send("Hello!");
+});
+
+app.on("listening", function(server){
+ console.log("My app is listening");
+});
+```
+
+
Commands
--------
View
@@ -6,8 +6,9 @@ var domain = require('domain')
, uuid = require('node-uuid')
, port = process.env.PORT || 3000
, timeout = process.env.SOCKET_TIMEOUT || 3000
- , cluster
- , isWorker = false;
+ , noop = function(){}
+ , isWorker = false
+ , cluster;
try {
cluster = require("cluster");
@@ -57,9 +58,12 @@ module.exports = function start(appPath) {
applyEvents(server, handler);
applyTimeouts(server);
+ // Let the app install any handlers (socket.io, sockjs, etc)
+ handler.emit("ready", server);
server.listen(port, function() {
console.log("Server listening on port " + port + (isWorker?" ("+cluster.worker.id+")":""));
+ handler.emit('listening', server);
});
});
@@ -72,6 +76,9 @@ module.exports = function start(appPath) {
* @api private
*/
function chooseHandlerType(app) {
+ // Default emit to noop
+ app.emit = app.emit || noop;
+
// Call our app handler in the req domain
if (app instanceof http.Server) {
// Express 2.x or Connect 1.x
@@ -135,8 +142,6 @@ function errorHandler(handler) {
res.end((isDev ? err.stack : "A fatal error occured"));
};
}
-
- var noop = function(){};
return function(req, res, reqd) {
return function(err) {
View
@@ -1,6 +1,6 @@
{
"name": "startup",
- "version": "0.1.6",
+ "version": "0.1.7",
"description": "Node.js HTTP app runner",
"scripts": {
"test": "./node_modules/.bin/mocha"

0 comments on commit f6b258f

Please sign in to comment.