简体   繁体   English

如何在 module.exports 中调用多个函数

[英]How to call multiple functions in module.exports

I have a list of functions that are in a separate js file.我有一个单独的 js 文件中的函数列表。 Entire file needs to be called in node.js.整个文件需要在 node.js 中调用。 These functions are responsible for creating j3pop headers.这些函数负责创建 j3pop 标头。 Here is what the code looks like:代码如下所示:

(function1{}())
(function2{}())
(function3{}())

How do I export them all and call in order?如何将它们全部导出并按顺序调用?

This is how I got third function in file:这就是我在文件中获得第三个 function 的方式:

module.exports = {   (function() {
      'use strict';
      console.log("inside");
      var afterReadyCbCalled = false;
      var originalHeaders = ["X-Origin-DC", "gytp","Cache-Control", "max-age=0","X-Forwarded-For", "103.255.4.53","X-Client-SrcPort", "45244","Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8","Accept-Language", "en-US,en;q=0.5","X-Forwarded-Proto", "https","X-TLS-Version", "771","Upgrade-Insecure-Requests", "1","ISTL-REFERER", "https://www.sahibinden.com/",];
      var originalBody = "";
      function afterReadyCb() {
        if (afterReadyCbCalled) return;
        afterReadyCbCalled = true;
        var xhr = new XMLHttpRequest();
        xhr.onload = function() {
          var isValid = xhr.getResponseHeader("ISTL-INFINITE-LOOP");
          if (isValid != null && isValid != '') return;
          var a = xhr.getResponseHeader("ISTL-REDIRECT-TO");
          if (a != null && a != '') {
            location.replace(a);
          } else {
            if (window.history != null && typeof history.replaceState === 'function') {
              var responseURL = xhr.responseURL != null ? xhr.responseURL : xhr.getResponseHeader("ISTL-RESPONSE-URL");
              if (responseURL != null && responseURL != '') {
                history.replaceState(null, '', responseURL);
              }
            }
            document.open();
            document.write(xhr.responseText);
            document.close();
          }
        };
        xhr.open("get", location.href, true);
        for (var i = 0; i < originalHeaders.length; i += 2) {
          var headerName = originalHeaders[i];
          try {
            xhr.setRequestHeader(headerName, originalHeaders[i + 1]);
          } catch (e) {}
        }
        xhr.setRequestHeader("ISTL-INFINITE-LOOP", '1');
        xhr.send(originalBody);
        var evt = document.createEvent('Event');
        evt.initEvent('QLpZFJdHv', true, true);
        dispatchEvent(evt);
      }
      addEventListener('afterReady', afterReadyCb, false);
      setTimeout(afterReadyCb, 400);
    }());

 }

Don't know about rest不知道rest

Would it be just:会不会只是:

module.exports = {
    (function() {
        (function() {})()
        (function() {})()
        (function() {})()
    })()
}

You call multiple functions in module.exports by doing this;通过这样做,您可以调用 module.exports 中的多个函数;

module.exports = {
   somefunction1,
   somefunction2,
   somefunction3,
}

function somefunction1(){
   // do stuffs
}
function somefunction2(){
   // do stuffs
}
function somefunction3(){
   // do stuffs
}

Then to import in another file do this;然后导入另一个文件执行此操作;

const anyName = require('./theFilePath');

anyName.somefunction1()

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

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