Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/f0330fb180dd58164b12e6d66f541da2 to your computer and use it in GitHub Desktop.
Save nhancv/f0330fb180dd58164b12e6d66f541da2 to your computer and use it in GitHub Desktop.
ExpressJs: Deal with client interrupt connection
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