Socket
Book a DemoInstallSign in
Socket

express-end

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-end

Express middleware to emit 'end' event on res.end()

0.0.8
latest
Source
npmnpm
Version published
Weekly downloads
9.2K
6.99%
Maintainers
1
Weekly downloads
 
Created
Source

npm version Build Status Coverage Status Code Climate Inch CI

Dependency Status devDependency Status

express-end

Express middleware to emit end event on res.end()

res object has following events:

  • header - when HTTP Header is written,
  • close - when connection is closed from client side
  • finish - when request processing finished (*)

(*) Unfortunately, if client has closed its connection, there is no event signalling that the server side finished the processing of the request as finish event does not fires in this case. It may be correct from abstract framework perspective as the request to be terminated without response to the client (and network connection is closed) and looks like it's does not matter whether the server finished the processing or not. However, there are several negative outcomes of this approach. One of them is that middleware has no information when the server has really finished the processing of the request.

This module overrides res.end() function to emit end when res.end() is called, i.e. when processing by server is finished independently of whether the connection was closed by client or not.

Usage example:

'use strict';

var express = require('express');
var http    = require('http');
var endMw = require('express-end');
//var endMw = require('../');
var app = express();

app.use(endMw);

var count = 0;

app.use(function(req, res, next) {
  var current = ++count;
  console.log('[%d] app.use()', current);

  res.once('close',  function() {
    console.log('[%d] app.use(): res.once(close)', current);
  });

  res.once('end',    function() {
    console.log('[%d] app.use(): res.once(end)', current);
  });

  res.once('finish', function() {
    console.log('[%d] app.use(): res.once(finish)', current);
  });

  next();
});


var httpPort = 8080;
var RESPONSE_DELAY = 1000; // Milliseconds

app.get('/test1', function (req, res) {
  var result = { test: 'test' };
  setTimeout(function() {
    res
      .status(200)
      .send(result);
  }, RESPONSE_DELAY);
});


var server = http.createServer(app);

server.listen(httpPort, function () {
  console.log('* Server listening at %s:%d', server.address().address, server.address().port);
});

Log (first request completed by server, second request cancelled by client):

$ node app.js 
* Server listening at :::8080
[1] app.use()
[1] app.use(): res.once(end)
[1] app.use(): res.once(finish)
[2] app.use()
[2] app.use(): res.once(close)
[2] app.use(): res.once(end)

The source code for this example is located in demo/ subdirectory of package

You may also have a look on the discussion on the topic at stackexchange: http://codereview.stackexchange.com/questions/20069/monkey-patching-extra-events-in-node-js-http-express

If you have different needs regarding the functionality, please add a feature request.

Installation

npm install --save express-end

Usage

Credits

Alexander

github.com   npmjs.com   travis-ci.org   coveralls.io   inch-ci.org

License

MIT

Keywords

express,middleware,mw,emit,end,event,res,finish,close,patch

FAQs

Package last updated on 10 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.