Last active
August 29, 2015 14:26
-
-
Save voodooattack/c9e7ccf2205816f5157d to your computer and use it in GitHub Desktop.
Forcing express.js to use Fibres for ALL REQUESTS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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