NodeJS send xhr response to unavailable client

I have a http-function that responses on a request as usual:

app.post('/file', function(res, req){
     write(req.body.file, function(err, id){
         if(!err)
            res.send({id:id});
         else
         {
             res.status(500);
             res.send({ error: 500 });
         }
     });
});

I post a file that is beeing written to the filesystem. When the file was written, I send a response back to the client. Now the client lost the connection to the internet, the server can't send the response, throws an error and gets killed.

Error:

Error: Request aborted
    at IncomingMessage.onReqAborted ....
....
http.js:689
     throw new Error('Can\'t set headers after they are sent.');

How can I catch this exception?

To catch those errors you can res.on('error', function(err) {}) or req.socket.on('error', function(err) {}) to catch lower-level errors, although I think the lower-level errors do bubble up to the request/response object also.

I should also point out that your route parameters are flipped, it should be (req, res), not (res, req).