Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What's the best way to access one Connect middleware from another?

What's the best way to access one Connect middleware from another?

Problem

I've written a Connect Middleware where I'd like to re-use the "send" method of the "static" Connect middleware if it's installed. What's the best way to get a hold of that code in my middleware? Currently I'm passing the express object use "use"ing my middleware, sort of like this:

app.use(myMiddleware(express, options));

then myMiddleware calls

express.static.send(req, res, next, ...)

That works, but feels a bit clunky. Is there a better way?

Problem courtesy of: Kevin Dente

Solution

// myMiddleware.js
var connect = require("connect");
var send = connect.static.send

// package.json
...
"dependencies": { 
   "connect": "1.x"
}

Simply, get it directly from connect, add connect as dependency to your module.

Solution courtesy of: Raynos

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What's the best way to access one Connect middleware from another?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×