不确定我在这里做错了什么?
错误:
/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)
app.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;
答案 0 :(得分:4)
要将function
作为模块对象返回,请将其设置为module.exports
:
module.exports = makeRequest;
exports
只是module.exports
的便利副本。
答案 1 :(得分:1)
您需要调用该函数。
makeRequest.makeRequest("Your message");
您还可以通过以下方式简化代码:
module.exports.makeRequest = function(message) {
//...
};
答案 2 :(得分:0)
我相信您正在app.js的第一行导入命名空间。该命名空间是make_request。 而且您还尝试调用名为makeRequest的函数,该函数位于命名空间make_request中。 试试这个:
makeRequest.makeRequest("message"); on line 3 and 4 of app.js