Last active
December 16, 2016 18:53
-
-
Save peterli888/ef5ebe3a04bd66bdc59f0df749196eda to your computer and use it in GitHub Desktop.
express.js single instance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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