Git Product home page Git Product logo

http-compose's Introduction

http-compose

minimal http handler composition

npm install --save http-compose

inspired by creationix/stack and the middleware format in yoshuawuyts/merry

example

const http = require('http')
const compose = require('http-compose')
const Send = require('http-sender')()

const requestHandler = compose([
  (req, res, context, next) => {
    if (req.url === '/first') next(null, 'first')
    else next()
  },
  (req, res, context, next) => {
    context.yes = true
    next()
  },
  (req, res, context, next) => {
    if (req.url === '/error') next(new Error('Bad request'))
    else next()
  },
  (req, res, context, next) => {
    if (req.url !== '/404') next(null, context.yes)
    else next()
  },
])

http.createServer((req, res) => {
  requestHandler(req, res, {}, Send(req, res))
}).listen(5000)

usage

compose = require('http-compose')

handler = compose(handlers)

where a handler function is of shape:

function handler (req, res, context, next) {
  next(err, value)
}

composes an Array of handler functions into a single handler function.

if any handler function passes either an err or value to next(), compose will skip any subsequent handlers and forward this to the final next() callback.

complementary modules

related modules

license

The Apache License

Copyright ยฉ 2017 Michael Williams

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

http-compose's People

Contributors

ahdinosaur avatar

Stargazers

Andrejs Agejevs avatar Jake Burden avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

http-compose's Issues

idea: duplex stream wrappers

a curious idea is using a series of (req, res) => (nextReq, nextRes) => {} wrappers instead of the (req, req, next) => {} stack

but not right meow. ๐Ÿฑ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.