Skip to content

Instantly share code, notes, and snippets.

@peterli888
Last active December 16, 2016 18:53
Show Gist options
  • Save peterli888/ef5ebe3a04bd66bdc59f0df749196eda to your computer and use it in GitHub Desktop.
Save peterli888/ef5ebe3a04bd66bdc59f0df749196eda to your computer and use it in GitHub Desktop.
express.js single instance
in express.js
var global_app;
function createApplication(ifglobal) {
if(ifglobal && global_app){
return global_app;
}
var app = function(req, res, next) {
app.handle(req, res, next);
};
if(!global_app)global_app=app;
//below not change anything
mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
}
main.js
var express = require('express');
var app = express();
route.js
var express = require('express');
var app = express(true); //will return the same app ,so you can use it anywhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment