0

不知道我在这里做错了什么?

错误:

/Users/user/node/app.js:3
makeRequest("Here's looking at you, kid");
^
TypeError: object is not a function
    at Object.<anonymous> (/Users/user/node/app.js:3:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

应用程序.js

var makeRequest = require('./make_request');

makeRequest("Here's looking at you, kid");
makeRequest("Hello, this is dog");

make_request.js

var http = require('http');

var makeRequest = function(message) {

    //var message = "Here's looking at you, kid.";
    var options = {
        host: 'localhost', port:8080, path: '/', method: 'POST'
    }

    var request = http.request(options, function(response) {
        response.on('data', function(data) {
            console.log(data);
        });
    });
    request.write(message);
    request.end();
};

exports = makeRequest;
4

3 回答 3

4

要将function作为模块对象返回,请将其设置为module.exports

module.exports = makeRequest;

exports只是一个方便的副本module.exports

于 2013-01-05T02:13:12.083 回答
1

您需要调用该函数。

makeRequest.makeRequest("Your message");

您还可以通过执行以下操作来简化代码:

module.exports.makeRequest = function(message) {
    //... 
};
于 2013-01-05T02:12:24.200 回答
0

我相信您正在 app.js 的第一行导入命名空间。这个命名空间是 make_request。您还试图调用一个名为 makeRequest 的函数,该函数位于命名空间 make_request 中。试试这个:

makeRequest.makeRequest("message"); on line 3 and 4 of app.js
于 2013-01-05T02:13:36.193 回答