node.js - Catching HTTP response event -
i building node web application (using express) , keep dry possible, automatically acquire database connection each request need release (back pool).
i manually in every route polluting code. figured should listen server events , release once response either finished generating or sending. options have? events (and inside object (app, request or whatever) triggered when response complete?
res inherited eventemitter, , can use default events can emit own ones.
1 of common finish (in node.0.8.12 , older end) event:
res.on('finish', function() { // request processing finished }); you can attach event requests if want, or specific in roots.
additionally can create middleware, db connection, , attach finish event , possibly timeouts. , need add middleware routes, minimum of repetitive coding.
in mean time, recommend not create db connection each request, big overhead , unnecessary use of sockets io handlers. can freely use onse single connection, , efficient performance point of view.
Comments
Post a Comment