Not Providing Milkshakes

    express-yields-2

    1.1.2 • Public • Published

    ExpressJS Yields

    A dead simple ES6 generators support hack for ExpressJS

    Usage

    npm install express-yields --save
    

    Then require this script somewhere before you start using it:

    const express = require('express');
    const yields = require('express-yields');
    const User = require('./models/user');
    const app = express();
     
    app.get('/users', function* (req, res) {
      const users = yield User.findAll(); // <- some Promise
      res.send(users);
    });

    A Notice About Calling next

    As we all know express sends a function called next into the middleware, which then needs to be called with or without error to make it move the request handling to the next middleware. It still works, but in case of a generator function, you don't need to do that. If you want to pass an error, just throw a normal exception:

    app.use(function * (req, res) {
      const user = yield User.findByToken(req.get('authorization'));
     
      if (!user) throw Error("access denied");
    });

    How Does This Work?

    This is a very minimalistic and unintrusive hack. Instead of patching all methods on an express Router, it wraps the Layer#handle property in one place, leaving all the rest of the express guts intact.

    The idea is that you require the patch once and then use the 'express' lib the usual way in the rest of your application.

    Copyright & License

    All code in this repository is released under the terms of the ISC license.

    Copyright (C) 2016 Nikolay Nemshilov

    Install

    npm i express-yields-2

    DownloadsWeekly Downloads

    2,421

    Version

    1.1.2

    License

    ISC

    Last publish

    Collaborators

    • scottmas