Sign In

express-yields-2

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-yields-2

ES6 generators and async functions support for expressjs

latest
Source
npmnpm
Version
1.1.2
Version published
Maintainers
1
Created
Source

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.

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

Copyright (C) 2016 Nikolay Nemshilov

Keywords

expressjs

FAQs

Package last updated on 30 Oct 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts