var express = require('express'); var app = express(), What is express()?? is it a method or a constructor? Where does it come from

后端 未结 5 1928
南方客
南方客 2020-12-02 14:30
var express = require(\'express\'); 
var app = express();

This is how we create an express application. But what is this \'express()\'? Is it a met

相关标签:
5条回答
  • 2020-12-02 15:25

    You’ll use Node’s require function to use the express module. require is similar to keywords like import or include in other languages. require takes the name of a package as a string argument and returns a package. There’s nothing special about the object that’s returned—it’s often an object, but it could be a function or a string or a number.

    var express = require('express'); 
    

    => Requires the Express module just as you require other modules and and puts it in a variable.

    var app = express(); 
    

    => Calls the express function "express()" and puts new Express application inside the app variable (to start a new Express application). It's something like you are creating an object of a class. Where "express()" is just like class and app is it's newly created object.

    By looking the code of express below you are good to go what is really happening inside.

    File 1: index.js

    'use strict';
    
    module.exports = require('./lib/express');
    

    File 2 : lib/express.js

    'use strict';
    
    var EventEmitter = require('events').EventEmitter;
    var mixin = require('merge-descriptors');
    var proto = require('./application');
    var Route = require('./router/route');
    var Router = require('./router');
    var req = require('./request');
    var res = require('./response');
    
    /**
     * Expose `createApplication()`.
     */
    
    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;
    }
    exports.application = proto;
    exports.request = req;
    exports.response = res;    
    exports.Route = Route;
    exports.Router = Router;
    });
    

    How require works

    When you call require('some_module') in node here is what happens:

    1. if a file called some_module.js exists in the current folder node will load that, otherwise:

    2. Node looks in the current folder for a node_modules folder with a some_module folder in it.

    3. If it doesn't find it, it will go up one folder and repeat step 2

    This cycle repeats until node reaches the root folder of the filesystem, at which point it will then check any global module folders (e.g. /usr/local/node_modules on Mac OS) and if it still doesn't find some_module it will throw an exception.

    0 讨论(0)
  • 2020-12-02 15:31

    Is it a method or a constructor?

    Neither; it's a function, although if you said "method" I don't think anyone would give you a hard time.

    A method is a function attached to an object. In JavaScript, methods are just mostly functions that you reference via object properties. (Update: As of ES2015, if you use method syntax to create them, they're slightly more than that because they have access to super.)

    A constructor, in JavaScript, is a function you call via the new operator. Even though other functions may create things, we don't typically call them "constructors" to avoid confusion. Sometimes they may be "creator" or "builder" functions.

    Where does it come from?

    ExpressJS is a NodeJS module; express is the name of the module, and also the name we typically give to the variable we use to refer to its main function in code such as what you quoted. NodeJS provides the require function, whose job is to load modules and give you access to their exports. (You don't have to call the variable express, you can do var foo = require('express'); and use foo instead, but convention is that you'd use the module's name, or if only using one part of a module, to use the name of that part as defined by the module's documentation.)

    0 讨论(0)
  • 2020-12-02 15:31

    Ancient post. I think the original poster was confused about why the syntax to call the function exported by module express is

    var app = express() 
    

    instead of

    var app = express.express()
    

    To clarify: require() function does not create a reference to that 'module'. There's no such thing as reference to a module. There's only reference to thing(s) exported by a module.

    require('xxx.js'), where the .js extension can be omitted, returns whatever is exported by that xxx.js file. If that xxx.js file exports an object, require('xxx.js') returns an object; if a function is exported, require('xxx.js') returns a function; if a single string is exported, require('xxx.js') returns a string...

    If you check source code of file express.js, you will see that it exports a single function. So in

    var express = require('express')
    

    The first express is assigned whatever is exported by module express, which in this case happens to be a single function. express is a function, not a reference to a module. Hence on second row you just invoke that function:

    var app = express()
    

    Hope this helps!

    0 讨论(0)
  • 2020-12-02 15:31

    let me answer this question by an example. create 2 javascript files. play1.js and express.js

    //express.js
    function createApplication(){
       var app = 'app';
        return app;
      }
    module.exports = createApplication;
    //keep in mind that we are not doing module.exports = {createApplication}
    

    now import express.js in play1.js file

    //play1.js
      var express = require('./express);
       var app = express();
     // this will call createApplication function as app is referencing to it.
       console.log(app); // "app"
    
    0 讨论(0)
  • 2020-12-02 15:31

    Whenever you import a module like

    const express = require('express')
    

    express is a module with functions or objects or variables assigned to it . take a look at /lib/express

    you are able to access the function createApplication inside express module as express() because the function is assigned directly to the module like

    exports = module.exports = createApplication;

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

    so you are able to access the function createApplication just calling express() as function

    now when you check out the other section of the express library, you can see a bunch of other objects attached to the exports special object as well.

    /**
     * Expose the prototypes.
     */
    
    exports.application = proto;
    exports.request = req;
    exports.response = res;
    
    /**
     * Expose constructors.
     */
    
    exports.Route = Route;
    exports.Router = Router;
    
     // other exports
    

    these objects or function assigned to export special object can be accessed from the import section using express as an object.

    express.{name}

    express.Route
    express.Router etc
    

    In the end you are just exporting a bunch of methods or objects that are attached to the module.export special object inside express js file

    to read more on module.export special object go here

    0 讨论(0)
提交回复
热议问题