Express: add `res.assert()` function

Created on 4 Dec 2018  路  3Comments  路  Source: expressjs/express

I can use single-line to ensure if a variable is true like this in Koa:

ctx.assert(ctx.state.user, 401, 'User not found. Please login!');

So I wonder that if we could add same method to res?

res.assert(ctx.state.user, 401, 'User not found. Please login!');

Most helpful comment

I love express because of it's built-in functions like view render, body-parser,static-server, router, so I hope you could add more great tools, but it's ok if you think res.assert should not be add as a built-in function.

All 3 comments

Hey @wxs77577, while I think this is an interesting sugar layer on top of responding conditionally based on an assertion, I am not a fan of adding things like this to express directly. I think this is a great thing as an separate library, maybe Koa even already published this as a stand alone module?

I love express because of it's built-in functions like view render, body-parser,static-server, router, so I hope you could add more great tools, but it's ok if you think res.assert should not be add as a built-in function.

Yes, let's keep core Express as bare-bones as possible.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guyisra picture guyisra  路  3Comments

HafidAbnaou picture HafidAbnaou  路  3Comments

despairblue picture despairblue  路  3Comments

haider0324 picture haider0324  路  3Comments

afanasy picture afanasy  路  3Comments