3

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.

In my weather.js file I have :

"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");.

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.

pourmesomecode
  • 4,108
  • 10
  • 46
  • 87

2 Answers2

4

I will first go through the details, then we will work down to answer your question. Its all about CommonJS standards. CommonJS standards specify the following three components when working with modules:

  1. require(): This method is used to load a module into your code.
  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().
  3. module.exports: Exposes the full module when it is loaded using 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:

//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:

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

Hope this helps.

2

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:

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.

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

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

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