Skip to content

Instantly share code, notes, and snippets.

@simone-sanfratello
Last active October 13, 2021 20:53
Show Gist options
  • Save simone-sanfratello/976b6e916e9dd4e985de3ba67cb842ab to your computer and use it in GitHub Desktop.
Save simone-sanfratello/976b6e916e9dd4e985de3ba67cb842ab to your computer and use it in GitHub Desktop.
server aborted request
const http = require('http')
const PORT = 3000
const server = http.createServer((req, res) => {
req.on('aborted', () => {
console.log('>>>>>> server event request aborted')
})
res.writeHead(200, { 'Content-Type': 'text/plain' })
setTimeout(() => {
res.end('done')
}, 5000)
})
server.listen(PORT, () => {
console.log('started')
const options = {
port: PORT,
host: 'localhost'
}
const req = http.request(options, (response) => {
let str = ''
response.on('data', function (chunk) {
str += chunk
})
response.on('end', function () {
console.log('request ends, received: ', str)
})
})
req.end()
req.on('error', (err) => {
console.log('request error event: ', err)
})
setTimeout(() => {
console.log('aborting request ...')
req.destroy()
}, 1000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment