Giter Club home page Giter Club logo

fluture-express's Introduction

Fluture Express

NPM Version Dependencies Build Status Code Coverage Greenkeeper badge

Create Express middleware using Futures from Fluture.

Usage

npm install --save fluture fluture-express

Allows for the definition of pure functions to be used as Express middleware. This has benefits for testing and developer sanity. Another benefit of this particular approach, where every middleware is wrapped individually, is that it plays nicely with existing Express middleware, and they can be used interchangably.

//index.js

const {dispatcher} = require('fluture-express');
const app = require('express')();
const dispatch = dispatcher('./actions');

app.use(dispatch('welcome'));
app.listen(3000);
//actions/welcome.js

const {Json} = require('fluture-express');
const Future = require('fluture');

module.exports = (req, locals) => Future.do(function*(){
  const user = yield locals.database.find('sessions', locals.session.id);
  return Json(200, {welcome: user.name});
});

For a more in-depth example, see the example directory.

Documentation

Pseudo types

Req

The Express Request object.

Res a

The Express Response object with a locals property of type a.

The Response type

Fluture-Express mutates the response object for you, based on a specification of what the response should be. This specification is captured by the Response sum-type. It has three constructors.

Indicates a streamed response. The first argument will be the response status code, the second will be used as a mime type, and the third will be piped into the response to form the response data.

Indicates a JSON response. The first argument will be the response status code, and the second will be converted to JSON and sent as-is.

Indicates that this middleware does not form a response. The supplied value will be assigned to res.locals and the next middleware will be called.

Middleware creation utilities

Converts an action to an Express middleware.

Takes a function that returns a Future of a Response, and returns an Express middleware that uses the returned structure to make the appropriate mutations to the res.

If the Future rejects, the rejection reason is passed into next for further error handling with Express.

Creates middleware that uses the export from the given file in the given directory as an "action".

It takes the file in two steps for convenience. You are encouraged to use the first parameter to set up a sub-directory where all your actions live.

The exported value should be a function of the same signature as given to middleware.

fluture-express's People

Contributors

avaq avatar

Watchers

 avatar

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.