middleware-helper

A library of helpful connect/express middleware functions.

npm install middleware-helper
2 downloads in the last week
2 downloads in the last month

middleware-helper

A library of connect/express middleware helper methods.

Example usage

Use methods defined in the library as middleware in a connect/express app to set up or end a request.

// Load dependencies.
var middleware_helper = require('../lib')
var express = require('express')

var routes = {
    "example_usage_handler": function(req, res, next) {
        res.locals.response_data.foo = 'bar'

        // Status code defaults to 500 (internal server error) if not set.
        res.locals.status = 200

        return next()
    }
}

var app = express()
app.use(express.logger('default'))
app.use(express.bodyParser())

// Handle request for `/example_usage` HTTP endpoint, piecing together steps
// in a flow for handling the request.  The routes.example_usage_handler
// method is responsible for business logic, while the middleware_helper
// methods are responsible for preparing, formatting, and sending the response.
app.get('/example_usage'
    , middleware_helper.setup
    , routes.example_usage_handler
    , middleware_helper.render_json
    , middleware_helper.end('application/json', 'utf8')
)

app.listen(8000)
console.log('Listening on port 8000.')

When a request is received this results in an updated header.

HTTP/1.1 200 OK
RequestID: 70ffc032c1b1537e11a5921d2005f277ff4aceff
RequestTimer-Delta: 0
Content-Type: application/json
...

{
    "foo": "bar"
}

License

This repository is made available to the public as open source software under the MIT License.

npm loves you