Created
January 2, 2018 10:43
-
-
Save nhancv/f0330fb180dd58164b12e6d66f541da2 to your computer and use it in GitHub Desktop.
ExpressJs: Deal with client interrupt connection
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
const express = require('express') | |
const app = express() | |
app.get('/', (req, res) => { | |
var connectionInterrupt = false; | |
var str = "a hihi do ngoc." + new Date().getTime() | |
console.log("Process: " + str); | |
req.connection.on('close',function(){ | |
connectionInterrupt = true; | |
}); | |
setTimeout(function(){ | |
res.send(str) | |
req.connection.removeAllListeners('close') | |
if(connectionInterrupt){ | |
console.log("Roll back: " + str); | |
} else { | |
console.log("Process done"); | |
} | |
}, 2000); | |
}) | |
var server = app.listen(3000, () => console.log('Example app listening on port 3000!')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment