简体   繁体   English

在Node.js中使用Exports返回值

[英]Using exports in nodejs to return a value

I've been doing some reading on modularizing my code and decided to export some functions to a separate file and include them in my main function once it's called. 我一直在阅读有关模块化代码的内容,并决定将一些函数导出到单独的文件中,并在调用它们后将其包含在我的主函数中。

So I have two files, index.js and weather.js . 所以我有两个文件, index.jsweather.js

In my weather.js file I have : 在我的weather.js文件中,我有:

"use strict"; “使用严格”;

exports.sometest = sometest;

function sometest() {
    request('http://api.openweathermap.org/data/2.5/weather?&appid=' + process.env.weather_api_key + '', (error, response, body) => {
      return body;
    });
}

Then in my index.js file I include it const testing = require("../../../lib/weather"); 然后在我的index.js文件中将其包含const testing = require("../../../lib/weather"); .

So how can I user the data I return within the function I've created? 那么如何在创建的函数中使用返回的数据呢? And could someone explain to me how it works, I'm a little confused in terms of scoping and syntax etc 有人可以向我解释它是如何工作的,在范围和语法等方面我有点困惑

Thank you. 谢谢。

I will first go through the details, then we will work down to answer your question. 我将首先详细介绍一下,然后我们将努力回答您的问题。 Its all about CommonJS standards. 其全部关于CommonJS标准。 CommonJS standards specify the following three components when working with modules: CommonJS标准在使用模块时指定了以下三个组件:

  1. require(): This method is used to load a module into your code. require():此方法用于将模块加载到您的代码中。
  2. exports: This object is contained in each module and allows to expose certain functionality of your code when the module is loaded using require(). 出口:此对象包含在每个模块中,并允许在使用require()加载模块时公开代码的某些功能。
  3. module.exports: Exposes the full module when it is loaded using require(). module.exports:使用require()加载完整模块时,将其公开。

By now you should have gathered that we always require the module, if we were to use method 2 (exposing certain functionality) this is what it would look like: 到现在为止,您应该已经收集到我们始终需要该模块,如果要使用方法2(公开某些功能),它将是这样:

//weather.js is the name of the file
exports.sometest = function() {
    request('http://api.openweathermap.org/data/2.5/weather?&appid=' + process.env.weather_api_key + '', (error, response, body) => {
    return body;
};

And then in your index file we would access this function like this: 然后在您的索引文件中,我们将像下面这样访问此函数:

const testing = require('../../../lib/weather.js');
// access the method from the 'testing' object
testing.sometest;

If you want to expose the full module: 如果要公开整个模块:

// weather.js
module.exports = function sometest() {
     request('http://api.openweathermap.org/data/2.5/weather?&appid=' + process.env.weather_api_key + '', (error, response, body) => {
     return body;
     });
};

In index.js it would look like this: 在index.js中,它看起来像这样:

const testing = require("../../../lib/weather");
weatherTest = testing();

Hope this helps. 希望这可以帮助。

I think you are not correctly exporting your module, and adding some extra possible issue there. 我认为您没有正确导出模块,并在其中添加了一些额外的可能问题。

The way you shold export your module in weather.js is: 保留在weather.js导出模块的方式是:

module.exports = function sometest() {
    request('http://api.openweathermap.org/data/2.5/weather?&appid=' + process.env.weather_api_key + '', (error, response, body) => {
      return body;
    });
}

The way you are including it in index.js is ok. 在index.js中包含它的方式是可以的。

You have to keep in mind that weather is returning a function, so you'll need to execute it. 您必须记住天气正在返回一个函数,因此您需要执行它。 But besides you are executing an async action and there you shold provide a callback to be called when te response is available. 但是除了执行异步操作外,还可以在响应可用时在其中提供要调用的回调。 So, i would do this: index.js 所以,我会这样做:index.js

const testing = require("../../../lib/weather")(callback) // callback is a function that will be executed when request in weather is done;

weather.js weather.js

module.exports = function(callback) {
        request('http://api.openweathermap.org/data/2.5/weather?&appid=' + process.env.weather_api_key + '', (error, response, body) => {
          callback(body);
        });
    }

So, what is happening here. 所以,这里发生了什么。 In the weather.js you are returning a function. 在weather.js中,您将返回一个函数。 That function executes an async request, and when the request is done, execute a callback provided by index.js In index.js, you require the file, and add extra parentheses to execute the function returned by weather, which besides executes the callback provided when you hace the response from async request. 该函数执行一个异步请求,并在请求完成后执行index.js提供的回调。在index.js中,您需要文件,并添加额外的括号以执行weather返回的函数,除了执行提供的回调外当您收到异步请求的响应时。

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

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