Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="https://github.com/Raynos/middleware" rel="noreferrer">middleware</a></p> <p>I'm halfway through separating the concept of middleware in a new project. </p> <p>Middleware allows you to define a stack of actions that you should flow through. Express servers themselves are a stack of middlewares.</p> <pre><code>// express var app = express(); // middleware var stack = middleware(); </code></pre> <p>Then you can add layers to the middleware stack by calling <code>.use</code></p> <pre><code>// express app.use(express.static(..)); // middleware stack.use(function(data, next) { next(); }); </code></pre> <p>A layer in the middleware stack is a function, which takes n parameters (2 for express, <code>req</code> &amp; <code>res</code>) and a <code>next</code> function.</p> <p>Middleware expects the layer to do some computation, augment the parameters and then call <code>next</code>.</p> <p>A stack doesn't do anything unless you handle it. Express will handle the stack every time an incoming HTTP request is caught on the server. With middleware you handle the stack manually.</p> <pre><code>// express, you need to do nothing // middleware stack.handle(someData); </code></pre> <p>A more complete example :</p> <pre><code>var middleware = require("../src/middleware.js"); var stack = middleware(function(data, next) { data.foo = data.data*2; next(); }, function(data, next) { setTimeout(function() { data.async = true; next(); }, 100) }, function(data) { console.log(data); }); stack.handle({ "data": 42 }) </code></pre> <p>In express terms you just define a stack of operations you want express to handle for every incoming HTTP request.</p> <p>In terms of express (rather than connect) you have global middleware and route specific middleware. This means you can attach a middleware stack to every incoming HTTP requests or only attach it to HTTP requests that interact with a certain route.</p> <p>Advanced examples of express &amp; middleware :</p> <pre><code>// middleware var stack = middleware(function(req, res, next) { users.getAll(function(err, users) { if (err) next(err); req.users = users; next(); }); }, function(req, res, next) { posts.getAll(function(err, posts) { if (err) next(err); req.posts = posts; next(); }) }, function(req, res, next) { req.posts.forEach(function(post) { post.user = req.users[post.userId]; }); res.render("blog/posts", { "posts": req.posts }); }); var app = express.createServer(); app.get("/posts", function(req, res) { stack.handle(req, res); }); // express var app = express.createServer(); app.get("/posts", [ function(req, res, next) { users.getAll(function(err, users) { if (err) next(err); req.users = users; next(); }); }, function(req, res, next) { posts.getAll(function(err, posts) { if (err) next(err); req.posts = posts; next(); }) }, function(req, res, next) { req.posts.forEach(function(post) { post.user = req.users[post.userId]; }); res.render("blog/posts", { "posts": req.posts }); } ], function(req, res) { stack.handle(req, res); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload