How do I abort http reply with express in the middle? In my case I serve data that's generated from external source and before I start serving it I know the size (content-length). However, if external source stops feeding me the data before feeding me full content I want to abort the request so that on receiver end there would be error event triggered.
Here's simple code, server:
//server
var express = require('express');
var app = express();
app.use(function(req, res, next){
res.setHeader('Content-Length', 100);
res.writeHead(200);
res.write('1234567890');
setTimeout(function() { res.connection.destroy(); }, 500);
});
app.listen(8080);
and
//client
var http = require('http');
http.get('http://localhost:8080/', function(res){
var length = 0;
console.log('expected size: ', res.headers['content-length']);
res.on('data', function(chunk){ length += chunk.length; });
res.on('end', function(){ console.log('received: ', length); });
}).on('error', function(e){
console.log('error', e);
});
It's expected that client should show error that connection was reset and not behave as everything was ok even though only 10 bytes out of 100 were received.
How to abort connection on sender side when it's known that request cannot be fulfilled? One way I found out is to throw exception inside that timer, then receiver will log error, but I don't want to throw exception, I want to do it properly.