简体   繁体   English

NodeJS中的exports语句

[英]The exports statement in NodeJS

I have the following code in server.js 我在server.js中有以下代码

var http = require('http');

function start() {
    function onRequest(request, response) {
        console.log('onrequest called');
        response.writeHead(200, { 'Content-type': 'text/plain' });
        response.write("Hello world!");
        response.end();
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server started!");
}

exports.start = start;

And the following in index.js 以及index.js中的以下内容

var server = require('./server');
server.start();

What I'm not understanding is how the line exports.start = start; 我不理解的是exports.start = start; is working. 正在工作。 Where is exports coming from? exports来自哪里? Why does index.js invoke the start method by server.start(); 为什么index.js通过server.start();调用start方法server.start(); rather than exports.start() ? 而不是exports.start() Isn't exports just a variable we put in the global namespace to make make a local var accessible to other modules by sticking it as a property on the global variable exports ? exports不仅仅是我们放在全局命名空间中的变量,通过将其作为全局变量exports的属性来使其他模块可以访问本地变量吗?

Help! 救命!

Node wraps each module in it's own IIFE that provides arguments such as module , exports , __dirname , etc. Node将每个模块包装在它自己的IIFE ,提供诸如moduleexports__dirname等参数。

So when you write: 所以当你写:

var http = require('http');

function start() {
    function onRequest(request, response) {
        console.log('onrequest called');
        response.writeHead(200, { 'Content-type': 'text/plain' });
        response.write("Hello world!");
        response.end();
    }

    http.createServer(onRequest).listen(8888);
    console.log("Server started!");
}

exports.start = start;

It's actually being wrapped in something like: 它实际上包含在以下内容中:

(function(module, exports, __dirname, ...) {
  var http = require('http');

  function start() {
      function onRequest(request, response) {
          console.log('onrequest called');
          response.writeHead(200, { 'Content-type': 'text/plain' });
          response.write("Hello world!");
          response.end();
      }

      http.createServer(onRequest).listen(8888);
      console.log("Server started!");
  }

  exports.start = start;
})(module, exports, __dirname, ...)

What I'm not understanding is how the line exports.start = start; 我不理解的是exports.start = start行; is working. 正在工作。 Where is exports coming from? 出口来自哪里?

exports is an object just like any other JS object. exports就像任何其他JS对象一样是一个对象。 You are attaching a reference to start on exports.start . 您正在附加一个对exports.start start的引用。

Why does index.js invoke the start method by server.start(); 为什么index.js通过server.start()调用start方法; rather than exports.start()? 而不是exports.start()?

Good question. 好问题。 Since exports is just an object, exports.start won't reference anything unless you provide that reference by requiring a module. 由于exports只是一个对象,因此除非您通过要求模块提供该引用,否则exports.start不会引用任何内容。

You can, however, do this if your objective is to not have a local variable. 但是,如果您的目标是没有局部变量,则可以执行此操作。

require('./server').start()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM