Skip to content

Instantly share code, notes, and snippets.

@shimataro
Created January 10, 2018 14:35
Show Gist options
  • Save shimataro/2d87f58a98e35133ced34ecc249b96bb to your computer and use it in GitHub Desktop.
Save shimataro/2d87f58a98e35133ced34ecc249b96bb to your computer and use it in GitHub Desktop.
res.finally()
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