Created
January 10, 2018 14:35
-
-
Save shimataro/2d87f58a98e35133ced34ecc249b96bb to your computer and use it in GitHub Desktop.
res.finally()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function middleware(req, res, next) { | |
// 正常/異常を問わず、終了時に必ず呼びたい関数 | |
res.finally = (callback) => { | |
res | |
.on("close", callback) // 異常終了 | |
.on("finish", callback); // 正常終了 | |
if (res.socket.destroyed) { | |
// すでに接続が切れていた | |
callback(); | |
} | |
// チェーン可能にする | |
return res; | |
}; | |
next(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment