How to overload app.listen functions in expressjs

I am trying to create a (mostly) factory function that sets up and creates an expressjs server for a dozen small specialized servers that I have. For part of this, I want to increase the listening function.

I would like to know how best to do this. I am also looking for options to reuse the design.

The server is created normally:

var httpServer = express(); ... 

Due to the way the express method is developed (not sure if I'm right), I cannot access {any} .prototype.listen. So I came up with two approaches.

Using an additional variable in the current area:

  var oldListen = httpServer.listen; httpServer.listen = function(callback){ ... oldListen.call(httpServer, options.port, options.host, function(){ ... if ( typeof callback == 'function' ) callback(); }); }; 

It works and is pretty straight forward, but then I have a variable wart. I also have a solution for closing, but I think it might be too dumb to be practical:

 httpServer.listen = (function(superListen){ return function(callback){ ... superListen.call(httpServer, options.port, options.host, function(){ ... if ( typeof callback == 'function' ) callback(); }); }; })(httpServer.listen); 

Both examples are part of the factory context, and I intentionally reduce the arguments passed to the function.

Any help would be appreciated.

+8
express
source share
4 answers

If you insist on "overload", make sure you implement the original trace (this is the nature of the overload). Express listen is just an alias node internal HTTP listening method :

 server.listen(port, [host], [backlog], [callback]); 

UPDATE: Express even suggests using the node API for custom implementations: http://expressjs.com/4x/api.html#app.listen

Otherwise, you must create your own listen method, which will be defined as follows:

 httpServer.myCustomListen = function (callback) { httpServer.listen.call(httpServer, options.port, options.host, callback); } 

The second option is your best choice, but in order for it to work, you must expand the express library. Express is open source and hosted on Github. Insert it and change as you wish. Periodically pull in new updates so that you do not update in the main library. I do this all the time with node modules.

There are two ways to do this:

  • You have full control over the configuration of the code, but you consider it necessary when you are in the know about code written by the original authors.

  • If you find a bug or create a great feature, you can send a migration request to benefit the community as a whole.

First you redo the repository, then take the URL of your fork , clone it, and then add the link to the original upstream repository:

 git clone [url_to your_fork] cd express git remote add upstream git@github.com:strongloop/express.git 

Then you can push changes to your own repo ( git push ). If you want to receive updates from the source repo, you can git pull upstream master up from the repository: git pull upstream master .

If you want to add your own fork express as an npm module for the project, you should use the following:

 npm install git://github.com/[your_user_name]/express.git --save 
+2
source share

As Victor pointed out, the express prototype is in express/lib/application.js . This file is used to create an expression and is exported through the application namespace in express/lib/express.js . Therefore, using express.appliction.listen you can reference the .listen function.

You can use this method: (similar to Victor method)

 var express = require('express'); express.application._listen = express.application.listen; express.application.listen = function(callback) { return this._listen(options.port, options.host, callback); }; 

You can also use the Lo-dash _.wrap if you do not want to store the base function yourself in a variable. It will look something like this:

 var express = require('express'); var _ = require('lodash'); express.application.listen = _.wrap(express.application.listen, function(listenFn) { return listenFn(options.port, options.host, callback); // Called with the same this }; 

However, using these methods, you will encounter the problems that you mentioned in your question (lifting variable, creating an additional variable). To solve this problem, I usually created my own subclass of express.application and replaced the .listen function in this subclass and suggested that expression use this subclass. However, due to the explicit structure of the current, you cannot replace express.application with your own subclass without overriding the express() function itself.

Therefore, I would do to take express.application.listen completely, since this is only 2 lines. It is pretty simple!

 var express = require('express'); var http = require('http'); express.application.listen = function(callback) { return http.createServer(this).listen(options.port, options.host, callback); }; 

You can even make the https option!

 var express = require('express'); var http = require('http'); var https = require('https'); express.application.listen = function(callback) { return (options.https ? http.createServer(this) : https.createServer({ ... }, this)) .listen(options.port, options.host, callback); }; 

Note. One other answer mentions the expression forking and its modification. I would have a hard time justifying this for such a small function.

+2
source share

You should be able to easily overload the express listening feature. You can access it in the following path to the object: express.application.listen

So you can implement something like this:

 var express = require('express'); express.application.baseListen = express.application.listen; express.application.listen = function(port) { console.log('Port is: ' + port); this.baseListen(port); }; 

The implementation of the listening function is located in the following path in the express module folder: node_modules\express\lib\application.js

+1
source share

Bind and listen for connections on this host and port. This method is identical to node http.Server # listen ().

 var express = require('express'); var app = express(); app.listen(3000); 

The application returned by express () is, in fact, a JavaScript function designed to pass node HTTP servers as a callback to process requests. This allows you to easily provide both HTTP and HTTPS versions of your application with the same code base, since the application does not inherit from them (this is just a callback):

 var express = require('express'); var https = require('https'); var http = require('http'); var app = express(); http.createServer(app).listen(80); https.createServer(options, app).listen(443); 

The app.listen () method is a convenience method for the following (if you want to use HTTPS or provide both options, use the above technique):

 app.listen = function(){ var server = http.createServer(this); return server.listen.apply(server, arguments); }; 

Link : http://expressjs.com/api.html

Hope this helps.

-one
source share

All Articles