express-streamline

Express monkey-patch to support Streamline syntax.

npm install express-streamline
18 downloads in the last week
34 downloads in the last month

Travis build status

Express-Streamline

Patch for Express to add support for Streamline syntax in Express apps.

Supports both Express 2 and Express 3.

Example

var express = require('express-streamline');
var app = express.createServer();

// ...

app.use(function (req, res, _) {
    if (req.session.userId) {
        req.currentUser = User.getById(req.session.userId, _);
    }
});

// ...

app.get('/photos', function (req, res, _) {
    var photos = req.currentUser.getPhotos(_);
    res.render('photos', {
        photos: photos,
    });
});

Installation

npm install express-streamline

Usage

You can either require() Express normally and then patch it:

var express = require('express');
require('express-streamline');

Or just require() this module, which returns the patched Express for convenience:

var express = require('express-streamline');

Then, you can write any and all Express handlers in Streamline syntax by just replacing next with _.

// middleware handlers:
app.use(function (req, res, _) { ... });
app.param('user', function (req, res, _, user) { ... });

// route handlers:
app.get('/:user', function (req, res, _) { ... });
app.post('/:user', function (req, res, _) { ... });
// ... (all verbs supported)

// error handlers:
app.use(function (err, req, res, _) { ... });
app.error(function (err, req, res, _) { ... }); // Express 2 only

By default, Streamlined middleware handlers will continue to the next middleware, while Streamlined route and error handlers won't. This is generally what you want, but you can specify whether next is called by explicitly returning true or false.

// middleware to blacklist banned IP addresses,
// but allow all other requests to pass through:
app.use(function (req, res, _) {
    var isBanned = dbs.bannedIPs.search(req.ips, _).length > 0;
    if (isBanned) {
        res.send(403);
        return false;   // end the response
    }
});

If you run into any issues, file a bug!

License

MIT. © 2012-2014 Aseem Kishore.

Credits

TJ Holowaychuk for the awesome Express, and Bruno Jouhier for the awesome Streamline.

npm loves you