What is happening when require(“http”).Server() is evaluated with an Express app as its argument?












4















I was reading the Socket.io Chat Demo here: http://socket.io/get-started/chat/
and I got confused when looking at their require statements.



var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendfile('index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
});


Am I correct in thinking that require("express") produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app) creates a http.Server object with all of its fields and functions.



If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.



So, my question is, what is really happening here?










share|improve this question

























  • You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport. require is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware.

    – adeneo
    Dec 15 '14 at 20:01













  • ExpressJS' app.listen() is just for convenience for simple applications. It uses http.Server(app), or rather http.createServer(app);, itself.

    – Jonathan Lonowski
    Dec 15 '14 at 20:36


















4















I was reading the Socket.io Chat Demo here: http://socket.io/get-started/chat/
and I got confused when looking at their require statements.



var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendfile('index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
});


Am I correct in thinking that require("express") produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app) creates a http.Server object with all of its fields and functions.



If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.



So, my question is, what is really happening here?










share|improve this question

























  • You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport. require is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware.

    – adeneo
    Dec 15 '14 at 20:01













  • ExpressJS' app.listen() is just for convenience for simple applications. It uses http.Server(app), or rather http.createServer(app);, itself.

    – Jonathan Lonowski
    Dec 15 '14 at 20:36
















4












4








4


1






I was reading the Socket.io Chat Demo here: http://socket.io/get-started/chat/
and I got confused when looking at their require statements.



var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendfile('index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
});


Am I correct in thinking that require("express") produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app) creates a http.Server object with all of its fields and functions.



If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.



So, my question is, what is really happening here?










share|improve this question
















I was reading the Socket.io Chat Demo here: http://socket.io/get-started/chat/
and I got confused when looking at their require statements.



var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendfile('index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
});


Am I correct in thinking that require("express") produces an executable Express function (with all of the necessary functions and fields that go with that), and that require("http").Server(app) creates a http.Server object with all of its fields and functions.



If so, I am confused because Express creates a server when we call the .listen function so it seems redundant and backwards to pass an Express app to an http module server.



So, my question is, what is really happening here?







javascript node.js express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 15 '14 at 20:14







Alex Booth

















asked Dec 15 '14 at 19:59









Alex BoothAlex Booth

3316




3316













  • You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport. require is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware.

    – adeneo
    Dec 15 '14 at 20:01













  • ExpressJS' app.listen() is just for convenience for simple applications. It uses http.Server(app), or rather http.createServer(app);, itself.

    – Jonathan Lonowski
    Dec 15 '14 at 20:36





















  • You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport. require is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware.

    – adeneo
    Dec 15 '14 at 20:01













  • ExpressJS' app.listen() is just for convenience for simple applications. It uses http.Server(app), or rather http.createServer(app);, itself.

    – Jonathan Lonowski
    Dec 15 '14 at 20:36



















You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport. require is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware.

– adeneo
Dec 15 '14 at 20:01







You're creating a webserver "instance" and passing it to sockec.io so it can use it for transport. require is just a function, if you return another function from the export, it can be called with arguments, and you can pass stuff to the middleware.

– adeneo
Dec 15 '14 at 20:01















ExpressJS' app.listen() is just for convenience for simple applications. It uses http.Server(app), or rather http.createServer(app);, itself.

– Jonathan Lonowski
Dec 15 '14 at 20:36







ExpressJS' app.listen() is just for convenience for simple applications. It uses http.Server(app), or rather http.createServer(app);, itself.

– Jonathan Lonowski
Dec 15 '14 at 20:36














2 Answers
2






active

oldest

votes


















2














The http server expects a function which has the following signature:



function(req, res)



require('express')(); will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.






share|improve this answer
























  • In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

    – hobbs
    Dec 15 '14 at 20:14











  • @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

    – Alex Booth
    Dec 15 '14 at 20:15











  • express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

    – Timothy Strimple
    Dec 15 '14 at 20:26











  • @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

    – Alex Booth
    Dec 16 '14 at 0:17











  • The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

    – Michael Lewis
    Sep 12 '17 at 10:14



















1














var app = require('express')(); //infact, app is a requestListener function. 

var http = require('http').Server(app); // infact, function Server eqs http.createServer;

// infact,app.listen eqs http.listen


other code from nodejs and express model



we can see, require("express")() return app is a function.



//express/lib/express.js   https://github.com/strongloop/express/blob/master/lib/express.js

var proto = require('./application');

exports = module.exports = createApplication;

function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};

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;
}


we can see, node.createServer eqs Server



//nodejs/lib/http.js  https://github.com/nodejs/node/blob/master/lib/http.js

exports.createServer = function(requestListener) {
return new Server(requestListener);
};


we can see, express app.listen eqs http.listen



//express/lib/application.js  https://github.com/strongloop/express/blob/master/lib/application.js

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





//nodejs/lib/_http_server.js https://github.com/nodejs/node/blob/master/lib/_http_server.js

function Server(requestListener) {
if (!(this instanceof Server)) return new Server(requestListener);
net.Server.call(this, { allowHalfOpen: true });

if (requestListener) {
this.addListener('request', requestListener);
}

this.httpAllowHalfOpen = false;

this.addListener('connection', connectionListener);

this.addListener('clientError', function(err, conn) {
conn.destroy(err);
});





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27492101%2fwhat-is-happening-when-requirehttp-server-is-evaluated-with-an-express-app%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    The http server expects a function which has the following signature:



    function(req, res)



    require('express')(); will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.






    share|improve this answer
























    • In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

      – hobbs
      Dec 15 '14 at 20:14











    • @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

      – Alex Booth
      Dec 15 '14 at 20:15











    • express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

      – Timothy Strimple
      Dec 15 '14 at 20:26











    • @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

      – Alex Booth
      Dec 16 '14 at 0:17











    • The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

      – Michael Lewis
      Sep 12 '17 at 10:14
















    2














    The http server expects a function which has the following signature:



    function(req, res)



    require('express')(); will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.






    share|improve this answer
























    • In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

      – hobbs
      Dec 15 '14 at 20:14











    • @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

      – Alex Booth
      Dec 15 '14 at 20:15











    • express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

      – Timothy Strimple
      Dec 15 '14 at 20:26











    • @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

      – Alex Booth
      Dec 16 '14 at 0:17











    • The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

      – Michael Lewis
      Sep 12 '17 at 10:14














    2












    2








    2







    The http server expects a function which has the following signature:



    function(req, res)



    require('express')(); will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.






    share|improve this answer













    The http server expects a function which has the following signature:



    function(req, res)



    require('express')(); will create a function with that signature which handles all the magic that express makes available like routing, middleware, etc. Express can create it's own http server instance, but since you're also using socket.io (which expects access to the http server as well), you'll need a separate http instance.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Dec 15 '14 at 20:04









    Timothy StrimpleTimothy Strimple

    18.5k45670




    18.5k45670













    • In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

      – hobbs
      Dec 15 '14 at 20:14











    • @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

      – Alex Booth
      Dec 15 '14 at 20:15











    • express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

      – Timothy Strimple
      Dec 15 '14 at 20:26











    • @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

      – Alex Booth
      Dec 16 '14 at 0:17











    • The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

      – Michael Lewis
      Sep 12 '17 at 10:14



















    • In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

      – hobbs
      Dec 15 '14 at 20:14











    • @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

      – Alex Booth
      Dec 15 '14 at 20:15











    • express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

      – Timothy Strimple
      Dec 15 '14 at 20:26











    • @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

      – Alex Booth
      Dec 16 '14 at 0:17











    • The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

      – Michael Lewis
      Sep 12 '17 at 10:14

















    In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

    – hobbs
    Dec 15 '14 at 20:14





    In fact Express .listen() returns the server, so you can get access to it that way, but then you would be accepting connections before socketio could do its thing.

    – hobbs
    Dec 15 '14 at 20:14













    @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

    – Alex Booth
    Dec 15 '14 at 20:15





    @Timothy: so does the variable app persist within both http and io? It seems like it would have to for routing instructions to work.

    – Alex Booth
    Dec 15 '14 at 20:15













    express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

    – Timothy Strimple
    Dec 15 '14 at 20:26





    express and socket.io override plug into the http server in different ways. For the most part, express just creates a middleware chain to pass the request / response through which will build up the response. Socket.io needs to plugin in front of that to ensure that its websocket upgrade requests aren't corrupted by any express middleware. Socket.io removes all the existing http handlers from the server, then only passes requests to express if it doesn't handle the request itself. See the implementation here: github.com/Automattic/engine.io/blob/master/lib/server.js#L342

    – Timothy Strimple
    Dec 15 '14 at 20:26













    @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

    – Alex Booth
    Dec 16 '14 at 0:17





    @TimothyStrimple: Thanks for the explanation--I think I've got some research to do on JS itself, but your answer helped me to much better understand what was going on in the case.

    – Alex Booth
    Dec 16 '14 at 0:17













    The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

    – Michael Lewis
    Sep 12 '17 at 10:14





    The major confusion point for me: app is a function. app has methods of its own, so it didn't look like a function, and everything I read suggested that http.createServer(app) needed a function.

    – Michael Lewis
    Sep 12 '17 at 10:14













    1














    var app = require('express')(); //infact, app is a requestListener function. 

    var http = require('http').Server(app); // infact, function Server eqs http.createServer;

    // infact,app.listen eqs http.listen


    other code from nodejs and express model



    we can see, require("express")() return app is a function.



    //express/lib/express.js   https://github.com/strongloop/express/blob/master/lib/express.js

    var proto = require('./application');

    exports = module.exports = createApplication;

    function createApplication() {
    var app = function(req, res, next) {
    app.handle(req, res, next);
    };

    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;
    }


    we can see, node.createServer eqs Server



    //nodejs/lib/http.js  https://github.com/nodejs/node/blob/master/lib/http.js

    exports.createServer = function(requestListener) {
    return new Server(requestListener);
    };


    we can see, express app.listen eqs http.listen



    //express/lib/application.js  https://github.com/strongloop/express/blob/master/lib/application.js

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





    //nodejs/lib/_http_server.js https://github.com/nodejs/node/blob/master/lib/_http_server.js

    function Server(requestListener) {
    if (!(this instanceof Server)) return new Server(requestListener);
    net.Server.call(this, { allowHalfOpen: true });

    if (requestListener) {
    this.addListener('request', requestListener);
    }

    this.httpAllowHalfOpen = false;

    this.addListener('connection', connectionListener);

    this.addListener('clientError', function(err, conn) {
    conn.destroy(err);
    });





    share|improve this answer






























      1














      var app = require('express')(); //infact, app is a requestListener function. 

      var http = require('http').Server(app); // infact, function Server eqs http.createServer;

      // infact,app.listen eqs http.listen


      other code from nodejs and express model



      we can see, require("express")() return app is a function.



      //express/lib/express.js   https://github.com/strongloop/express/blob/master/lib/express.js

      var proto = require('./application');

      exports = module.exports = createApplication;

      function createApplication() {
      var app = function(req, res, next) {
      app.handle(req, res, next);
      };

      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;
      }


      we can see, node.createServer eqs Server



      //nodejs/lib/http.js  https://github.com/nodejs/node/blob/master/lib/http.js

      exports.createServer = function(requestListener) {
      return new Server(requestListener);
      };


      we can see, express app.listen eqs http.listen



      //express/lib/application.js  https://github.com/strongloop/express/blob/master/lib/application.js

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





      //nodejs/lib/_http_server.js https://github.com/nodejs/node/blob/master/lib/_http_server.js

      function Server(requestListener) {
      if (!(this instanceof Server)) return new Server(requestListener);
      net.Server.call(this, { allowHalfOpen: true });

      if (requestListener) {
      this.addListener('request', requestListener);
      }

      this.httpAllowHalfOpen = false;

      this.addListener('connection', connectionListener);

      this.addListener('clientError', function(err, conn) {
      conn.destroy(err);
      });





      share|improve this answer




























        1












        1








        1







        var app = require('express')(); //infact, app is a requestListener function. 

        var http = require('http').Server(app); // infact, function Server eqs http.createServer;

        // infact,app.listen eqs http.listen


        other code from nodejs and express model



        we can see, require("express")() return app is a function.



        //express/lib/express.js   https://github.com/strongloop/express/blob/master/lib/express.js

        var proto = require('./application');

        exports = module.exports = createApplication;

        function createApplication() {
        var app = function(req, res, next) {
        app.handle(req, res, next);
        };

        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;
        }


        we can see, node.createServer eqs Server



        //nodejs/lib/http.js  https://github.com/nodejs/node/blob/master/lib/http.js

        exports.createServer = function(requestListener) {
        return new Server(requestListener);
        };


        we can see, express app.listen eqs http.listen



        //express/lib/application.js  https://github.com/strongloop/express/blob/master/lib/application.js

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





        //nodejs/lib/_http_server.js https://github.com/nodejs/node/blob/master/lib/_http_server.js

        function Server(requestListener) {
        if (!(this instanceof Server)) return new Server(requestListener);
        net.Server.call(this, { allowHalfOpen: true });

        if (requestListener) {
        this.addListener('request', requestListener);
        }

        this.httpAllowHalfOpen = false;

        this.addListener('connection', connectionListener);

        this.addListener('clientError', function(err, conn) {
        conn.destroy(err);
        });





        share|improve this answer















        var app = require('express')(); //infact, app is a requestListener function. 

        var http = require('http').Server(app); // infact, function Server eqs http.createServer;

        // infact,app.listen eqs http.listen


        other code from nodejs and express model



        we can see, require("express")() return app is a function.



        //express/lib/express.js   https://github.com/strongloop/express/blob/master/lib/express.js

        var proto = require('./application');

        exports = module.exports = createApplication;

        function createApplication() {
        var app = function(req, res, next) {
        app.handle(req, res, next);
        };

        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;
        }


        we can see, node.createServer eqs Server



        //nodejs/lib/http.js  https://github.com/nodejs/node/blob/master/lib/http.js

        exports.createServer = function(requestListener) {
        return new Server(requestListener);
        };


        we can see, express app.listen eqs http.listen



        //express/lib/application.js  https://github.com/strongloop/express/blob/master/lib/application.js

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





        //nodejs/lib/_http_server.js https://github.com/nodejs/node/blob/master/lib/_http_server.js

        function Server(requestListener) {
        if (!(this instanceof Server)) return new Server(requestListener);
        net.Server.call(this, { allowHalfOpen: true });

        if (requestListener) {
        this.addListener('request', requestListener);
        }

        this.httpAllowHalfOpen = false;

        this.addListener('connection', connectionListener);

        this.addListener('clientError', function(err, conn) {
        conn.destroy(err);
        });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 30 '15 at 3:11

























        answered Oct 30 '15 at 2:42









        acjialirenacjialiren

        916




        916






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f27492101%2fwhat-is-happening-when-requirehttp-server-is-evaluated-with-an-express-app%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]