简体   繁体   中英

Will emitting request and response handler in Express.js cause problem?

I have a doubt in my design pattern on my Express app, so i wrap my controller in try and catch, and the catch method is emitting (req, res) handler from controller and later will be handled by a function that send response back to the client. the code is more or less like this :

 const errorExceptionHandler = fn => (req, res, next) => { fn(req, res, next).catch((err) => { emitter.emit('onControllerError', { err: err, req: req, res: res, next: next }) }) } 

the code above emtting req, res, and next, the default parameters that express provided.

 emitter.on('onControllerError', params => { const err = params.err const req = params.req const res = params.res const next = params.next if (!res.headerSent) { res.send({ status: 500, url: process.env.DEBUG ? req.url : undefined, message: process.env.DEBUG ? err.message : "Something went wrong!" }) } }) 

and above is how the 'onControllerError' event is handled, my concern is, will this cause trouble later if the traffic goes up? or will it send a wrong response to the client?

Increased traffic wouldn't matter here as each request is still handled independently, plus all the necessary data is being passed directly to the event handler.

So no, based on your code I can't think of any reason why it would start to fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM