Skip to content

Instantly share code, notes, and snippets.

@voodooattack
Last active August 29, 2015 14:26
Show Gist options
  • Save voodooattack/c9e7ccf2205816f5157d to your computer and use it in GitHub Desktop.
Save voodooattack/c9e7ccf2205816f5157d to your computer and use it in GitHub Desktop.
Forcing express.js to use Fibres for ALL REQUESTS.
import Fiber from 'fibers';
// Modify express to always spawn a new fibre if none are active.
export default function (router) {
var oldProcess = router.process_params;
if (oldProcess)
router.process_params = function(...args) {
if (!Fiber.current)
Fiber(oldProcess.bind(router, ...args)).run();
else
oldProcess.apply(router, args);
};
return router;
}
import express from 'express';
import fiberify from './fiberify.js';
import wait from 'wait.for';
import fs from 'fs';
let app = fiberify(express()); // Don't forget to also do this for every router you create with express.Router()
app.get('/', function(req, res, next) { // This is already running inside a fibre.
let content = wait.for(fs.readFile, 'index.html');
res.send(content);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment