【问题标题】:NodeJS module exportNodeJS 模块导出
【发布时间】:2015-11-10 00:04:24
【问题描述】:

我有一个简单的 http 客户端模块 (api.js),它返回如下所示的承诺:

 exports.endpoint = '';

        exports.GET =  function(args){
            args.method = 'GET';
            args.uri = this.endpoint + args.uri;
            return asyncApiCall(args);
        };
        exports.POST =  function(args){
            args.method = 'POST';
            args.uri = this.endpoint + args.uri;
            return asyncApiCall(args);
        };
        exports.PUT =  function(args){
            args.method = 'PUT';
            args.uri = this.endpoint + args.uri;
            return asyncApiCall(args);
        };
        exports.DELETE= function(args){
            args.method = 'DELETE';
            args.uri = this.endpoint + args.uri;
            return asyncApiCall(args);
        };

        var asyncApiCall = function(args){
            var rp = require('request-promise');
            var options = {
            method: args.method,
            uri: args.uri,
            body : args.body,
            json: args.json
        }

        return rp(options);

        };

我使用这样的模块:

var api = require('./api.js');
var args = {
    uri : '/posts'

}
api.endpoint = 'http://localhost:3000';
api.GET(args)
    .then(function(res){
                console.log(res);
            }, function(err){
                console.log(err);
            });

现在,我想尽可能地改进模块。有什么办法不重复export.functionName?我在 NodeJS 中找到了 module.exports,但我不确定在这种情况下如何使用它。如何在 asyncApiCall 函数中设置端点变量一次,而不是在所有其他返回 asyncApiCall 的函数中设置?

【问题讨论】:

    标签: javascript node.js httpclient


    【解决方案1】:

    只是另一种风格:

    var rp = require('request-promise'); // Put it here so you don't have to require 1 module so many times.
    
    var asyncApiCall = function(args) {
      var options = {
        method: args.method,
        uri: args.uri,
        body : args.body,
        json: args.json
      };
      return rp(options);
    };
    
    // Let's hack it.
    var endpoint = '';
    var apis = {};
    ['GET', 'POST', 'PUT', 'DELETE'].forEach(function(method) {
      apis[method] = function(args) {
        args.method = method;
        args.uri = endpoint + args.uri;
        return asyncApiCall(args);
      }
    });
    
    module.exports = apis;
    module.exports.endpoint = '';
    

    【讨论】:

    • 我这里有问题。无法再按照我想要的方式设置端点(api.endpoint = 'localhost:3000';)请检查我的问题
    • 好的,明白了!端点需要“this”。我编辑了你的答案
    【解决方案2】:

    很多人选择将他们的导出方法放在一个新对象上并通过 module.exports 导出,例如

    var myExports = {
       get: function () {},
       post: function () {}
    }
    module.exports = myExports;
    

    As for module.exports vs exports

    看起来它可能适合设置一个完整的构造函数,并将你的方法绑定到它,如下所示:

    var requests = function (endpoint) {
       this.endpoint = endpoint;
    }
    
    requests.prototype.GET = function (args) {
        args.method = 'GET';
        args.uri = this.endpoint + args.uri;
        return asyncApiCall(args);
    }
    
    // And so on
    
    module.exports = requests;
    

    然后这样称呼它:

    var api = require('./api.js');
    var endpoint = new api("http://localhost:3000");
    
    endpoint.GET()
    

    【讨论】:

    • 第一个例子会导致模块不导出任何东西。
    • @FelixKling 谢谢。很好的收获。
    【解决方案3】:

    将其包装在一个类中并导出它的一个新实例

    function Module() {
    
    }
    
    Module.prototype.GET = function () {}
    
    module.export = new Module()
    // or
    module.export = Module
    // to call the constructor for your endpoint variable.
    

    【讨论】:

      猜你喜欢
      • 2021-10-12
      • 2016-09-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多