Express's Middleware type
See original GitHub issueIn my app, I’m using following error handler, which would break in case of err === null
. Why is the error
parameter allowed to be null
in Middleware
type?
export default function errorHandler(err, req, res, next) {
const errorDetails = err.stack || err;
...
}
I would rather see something like this:
declare type NextFunction = () => mixed;
declare type NextFunctionWithError = (err?: Error) => mixed;
declare type Middleware =
((req: Request, res: Response, next: NextFunction) => mixed) |
((error: Error, req : Request, res: Response, next: NextFunctionWithError) => mixed);
What do you think?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:25 (12 by maintainers)
Top Results From Across the Web
Is there a type in @types/express for Express middleware ...
use('/foo', <MiddleWareFn> function(req,res,next){});. however I am wondering if Express has typings for middleware functions already? node.js ...
Read more >Complete Guide to Express Middleware - Reflectoring
Middleware in Express are functions that come into play after the server receives the request and before the response is sent to the...
Read more >How to write Express.js middleware with TypeScript
A “middleware” in Express is just a function that intercepts every request so that you can return a custom response or apply a...
Read more >Middleware in Express.js - GeeksforGeeks
Middleware in Express.js · Middleware can process request objects multiple times before the server works for that request. · Middleware can be ...
Read more >Build and Understand Express Middleware through Examples
Express middleware are functions that execute during the lifecycle of a request to the Express server. Each middleware has access to the HTTP ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hmmm… this is a tough one. I think you could improve this by making sure to define that class as extending the base class and make sure that you add the type definitions for them to middleware functions:
I think we will probably have to add parameterization to the lib defs, but I am not sure what the best way to do this. We should look to see how definitely-typed accomplished this and see if we could follow their lead.
I’ll have to look at your #508 mod more closely. I’ve pulled out most of my app-specific stuff, so you can see how I did the wrapping: