Middleware and bodyParser / connect-form?

183 views
Skip to first unread message

Brandon Martin

unread,
Jun 12, 2011, 10:01:33 AM6/12/11
to expre...@googlegroups.com
I am working on a setup that I am using bodyParser and connect-form. I read all about how connect-form needs to be below bodyParser in the stack and I have all that working, but with one piece of middleware a restrictToSelf it locks up like req.form.complete is never called or anything. Here are some code snippets:

// app.js
app.configure(function(){
  app.use(express.bodyParser());
  app.use(form({ keepExtensions: true }));
});

app.post('/admin/account', loadUser, requireLogin, restrictToSelf, admin.account_update);

// middleware.js
exports.restrictToSelf = function (req, res, next) {
  Account.findOne({ "users.email" : req.current_user.email }, function(err, account) {
    if (account && account.subdomain == req.headers.host) {
      next();
    } else {
      req.flash('error', 'You are not authorized to access that page');
      res.redirect('back');
    }
  });
}

// controller.js
exports.account_update = function(req, res, next) {
  req.form.complete(function(err, fields, files){
    // do some upload stuff here
    res.redirect('back');
  });
};

I thought that middleware would be checked before the route is ever hit. I can get the same functionality working by implementing the restrictToSelf in the route, but I would like to keep it in the middleware as I use that for other routes. Any thoughts?

Thanks

--
Brandon Martin

vision media [ Tj Holowaychuk ]

unread,
Jun 13, 2011, 2:53:41 AM6/13/11
to expre...@googlegroups.com
looks like it's because when you introduce async stuff in that middleware, so while that middleware is doing it's thing you are actually losing "data" events

--
You received this message because you are subscribed to the Google Groups "Express" group.
To post to this group, send email to expre...@googlegroups.com.
To unsubscribe from this group, send email to express-js+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/express-js?hl=en.



--
Tj Holowaychuk
Vision Media
President & Creative Lead

Adam McDonald

unread,
Jul 25, 2011, 9:19:43 PM7/25/11
to expre...@googlegroups.com
Hmm, I'm having this exact same problem with the async middleware. Do you have any suggestions around it?

TJ Holowaychuk

unread,
Jul 25, 2011, 10:22:53 PM7/25/11
to expre...@googlegroups.com
take a look at connect's utils.js, there's a pause() util to buffer those events, far from ideal but to provide a linear API it's necessary for now

-- 
TJ Holowaychuk

On Monday, July 25, 2011 at 6:19 PM, Adam McDonald wrote:

Hmm, I'm having this exact same problem with the async middleware. Do you have any suggestions around it?

--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/hZVDryyTduMJ.

Brandon Martin

unread,
Jul 25, 2011, 10:29:13 PM7/25/11
to expre...@googlegroups.com

Thanks TJ I will take a look at that. I just didn't didn't include the middleware in that route and did the restrictToSelf logic in the request.form which is not ideal but it works.

--
Brandon Martin
Zyph | Martin Design Studios
Lead Developer
P - 509.991.7587
E - bma...@zyphmartin.com
http://zyphmartin.com

Adam McDonald

unread,
Jul 25, 2011, 10:38:35 PM7/25/11
to expre...@googlegroups.com
Hey TJ, would these modifications be to connect-form.js? I'm not positive when to call the resume() function due to the various callbacks.

TJ Holowaychuk

unread,
Jul 25, 2011, 10:40:20 PM7/25/11
to expre...@googlegroups.com
nope it's on other middleware that are async and used for POST/PUT etc, which unfortunately is anything doing broad work like authority checks from a db etc. 

-- 
TJ Holowaychuk

On Monday, July 25, 2011 at 7:38 PM, Adam McDonald wrote:

Hey TJ, would these modifications be to connect-form.js? I'm not positive when to call the resume() function due to the various callbacks.

--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/z9_vxJ6cLZoJ.

Adam McDonald

unread,
Jul 25, 2011, 10:53:19 PM7/25/11
to expre...@googlegroups.com
Okay. So, can you guide me in the right direction to use the pause/resume functions? I'm using async middleware to load the user and authentication, similar to @zmbartin.

TJ Holowaychuk

unread,
Jul 25, 2011, 11:04:27 PM7/25/11
to expre...@googlegroups.com
I think only the session middleware uses it right now, since most that do async are GET/HEAD only like static() etc. Basically you pause until the async stuff is done, then resume()

-- 
TJ Holowaychuk

On Monday, July 25, 2011 at 7:53 PM, Adam McDonald wrote:

Okay. So, can you guide me in the right direction to use the pause/resume functions? I'm using async middleware to load the user and authentication, similar to @zmbartin.

--
You received this message because you are subscribed to the Google Groups "Express" group.
To view this discussion on the web visit https://groups.google.com/d/msg/express-js/-/APtM0Xhg4T0J.

Adam McDonald

unread,
Jul 26, 2011, 2:15:30 AM7/26/11
to expre...@googlegroups.com
Excellent, TJ!! Thank you very much for your help.
Reply all
Reply to author
Forward
0 new messages