Javascript var express = require('express'); var app = express(), express() 是什么??它是一个方法还是一个构造函数?它从何而来

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27599614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:22:21  来源:igfitidea点击:

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

javascriptnode.jsexpress

提问by sreesreenu

var express = require('express'); 
var app = express();

This is how we create an express application. But what is this 'express()'? Is it a method or a constructor? Where does it come from??

这就是我们创建快速应用程序的方式。但是这个'express()'是什么?它是一个方法还是一个构造函数?它从何而来??

回答by T.J. Crowder

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 methodis a function attached to an object. In JavaScript, methods are justmostly functions that you reference via object properties. (Update: As of ES2015, if you use method syntax to create them, they'reslightly more than that because they have access to super.)

方法是附加到一个对象的功能。在JavaScript中,方法是只需大多是你通过对象属性引用功能。(更新:从 ES2015 开始,如果您使用方法语法来创建它们,它们会稍微多一点,因为它们可以访问super.)

A constructor, in JavaScript, is a function you call via the newoperator. 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.

一个构造函数,在JavaScript中,你是通过调用一个函数new运算符。尽管其他函数可能会创建事物,但我们通常不会称它们为“构造函数”以避免混淆。有时它们可​​能是“创建者”或“构建者”功能。

Where does it come from?

它从何而来?

ExpressJS is a NodeJS module; expressis 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 requirefunction, whose job is to load modules and give you access to their exports. (You don't haveto call the variable express, you can do var foo = require('express');and use fooinstead, 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.)

ExpressJS 是一个NodeJS 模块express是模块的名称,也是我们通常赋予变量的名称,用于在代码中引用其主要功能,例如您引用的内容。NodeJS 提供了require函数,其工作是加载模块并让您访问它们的导出。(你不会打电话给变量express,你可以做var foo = require('express');,并使用foo替代,但惯例是,你使用模块的名称,或者如果只使用一个模块的一部分,使用由定义那部分的名称模块的文档。)

回答by Rajdeep Gautam

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.

您将使用 Node 的 require 函数来使用 express 模块。require 类似于其他语言中的关键字,如 import 或 include。require 将包的名称作为字符串参数并返回一个包。返回的对象没有什么特别之处——它通常是一个对象,但也可以是一个函数、一个字符串或一个数字。

var express = require('express'); 

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

=> 需要 Express 模块,就像您需要其他模块一样,并将其放入变量中。

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.

=> 调用 express 函数“express()”并将新的 Express 应用程序放入 app 变量中(以启动一个新的 Express 应用程序)。这就像你正在创建一个类的对象。其中“express()”就像类,而应用程序是新创建的对象。

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

通过查看下面的 express 代码,您可以了解内部真正发生的事情。

File 1: index.js

文件 1:index.js

'use strict';

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

File 2 : lib/express.js

文件 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:

当您在 node 中调用 require('some_module') 时,会发生以下情况:

  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

  1. 如果当前文件夹节点中存在名为 some_module.js 的文件,则将加载该文件,否则:

  2. Node 在当前文件夹中查找 node_modules 文件夹,其中包含 some_module 文件夹。

  3. 如果它没有找到它,它会上升一个文件夹并重复第 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.

这个循环一直重复,直到 node 到达文件系统的根文件夹,此时它会检查任何全局模块文件夹(例如 Mac OS 上的 /usr/local/node_modules),如果它仍然没有找到 some_module 它将抛出异常.

回答by Rui Wang

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

古帖。我认为原始海报对为什么调用模块 express 导出的函数的语法是

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() 函数不会创建对该“模块”的引用。没有引用模块这样的东西。只有对模块导出的事物的引用。

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...

require('xxx.js'),其中 .js 扩展名可以省略,返回该 xxx.js 文件导出的任何内容。如果 xxx.js 文件导出一个对象,则 require('xxx.js') 返回一个对象;如果一个函数被导出,require('xxx.js') 返回一个函数;如果导出单个字符串,则 require('xxx.js') 返回一个字符串...

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

如果您检查文件 express.js 的源代码,您将看到它导出单个函数。所以在

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:

第一个 express 被分配了模块 express 导出的任何内容,在这种情况下恰好是单个函数。express 是一个函数,而不是对模块的引用。因此,在第二行,您只需调用该函数:

var app = express()

Hope this helps!

希望这可以帮助!

回答by Manish Sakariya

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

让我通过一个例子来回答这个问题。创建 2 个 javascript 文件。play1.js 和 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 文件中导入 express.js

//play1.js
  var express = require('./express);
   var app = express();
 // this will call createApplication function as app is referencing to it.
   console.log(app); // "app"

回答by Shersha Fn

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

express 是一个具有分配给它的函数或对象或变量的模块。看看/lib/express

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

您可以像express()一样访问express 模块中的createApplication函数,因为该函数是直接分配给模块的,例如

exports = module.exports = createApplication;

出口 = 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 createApplicationjust calling express() as function

所以你可以访问函数createApplication只需调用 express() 作为函数

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.

现在,当您查看 express 库的其他部分时,您还可以看到附加到导出特殊对象的一堆其他对象。

/**
 * 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 作为对象从导入部分访问。

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

最后你只是导出一堆方法或对象,这些方法或对象附加到 express js 文件中的 module.export 特殊对象

to read more on module.export special object go here

要阅读有关 module.export 特殊对象的更多信息,请转到此处