selfcontained[web development]

using express with broadway

Thursday September 20, 2012
By Brad Harris

Express is by no doubt an extremely popular http application server/framework for node.js. In this article I'd like to demonstrate how you can take advantage of broadway and express together.

In this approach, we'll treat the http server that express provides as just another plugin to our broadway application.

express http plugin

First we'll create an http plugin encapsulating express.

// http.js

var express = require('express');

var HttpPlugin = exports; = 'http';

HttpPlugin.attach = function(options) {
    var app = this;

    var server = app.http = express();


HttpPlugin.init = function(done) {

I'd also suggest defining your routes in their own module(s), and passing along the broadway application. This will allow them to take full advantage of any other functionality your application acquires through other plugins.

// routes.js
module.exports = function(app) {

    app.http.get('/', function(req, res){
        res.send('express on broadway');



Next we'll create a broadway application and toss in our http plugin.

// app.js

var broadway = require('broadway'),
    app = new broadway.App();


app.init(function(err) {

You can also just module.exports = app.http if you're using something like up and need to export an http server from your main applicaiton.


Finally, fire up your application

>node app.js

That's all there is to it. In this example, the http server is just one piece of our application, and we can bolt on additional functionality as needed through more plugins.

blog comments powered byDisqus