0

Not sure what I am doing wrong here?

Error:

/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;
ian
  • 11,605
  • 25
  • 69
  • 96

3 Answers3

4

To return the function as the module object, set it to module.exports:

module.exports = makeRequest;

exports is just a convenience copy of module.exports.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Better but still: ` events.js:71 throw arguments[1]; // Unhandled 'error' event ^ Error: connect ECONNREFUSED at errnoException (net.js:770:11) at Object.afterConnect [as oncomplete] (net.js:761:19)` – ian Jan 05 '13 at 02:18
  • Tried those but similar error: `events.js:71 throw arguments[1]; // Unhandled 'error' event ^ Error: getaddrinfo ENOENT at errnoException (dns.js:31:11) at Object.onanswer [as oncomplete] (dns.js:123:16)` – ian Jan 05 '13 at 02:45
  • I don't really know. This is a `node.js` tutorial I am trying to do so I am very basic with it. It does look like some kind of error with the address though. – ian Jan 05 '13 at 03:17
  • I found this https://groups.google.com/forum/?fromgroups=#!topic/nodejs/kL7dZrUO89M So it is some connection issue but not sure how to diagnose or fix it. – ian Jan 05 '13 at 03:26
  • Not working but that fixed that particular error so I will make a new question for the new error. – ian Jan 05 '13 at 03:49
  • The code works fine for me if I change `exports =` to `module.exports =`. It seems to me that there is a problem when DNS tries to resolve your hostname (which I am assuming is a website - not localhost). Have you looked at this post? http://stackoverflow.com/questions/9580226/nodejs-httprequest-with-data-getting-error-getaddrinfo-enoent – Will C. Jan 05 '13 at 04:29
1

You need to call the function.

makeRequest.makeRequest("Your message");

You can also simplify your code by doing:

module.exports.makeRequest = function(message) {
    //... 
};
Nix
  • 57,072
  • 29
  • 149
  • 198
  • Tried that but got this: `/Users/user/node/app.js:3 makeRequest.makeRequest("Here's looking at you, kid"); ^ TypeError: Object # has no method 'makeRequest' at Object. (/Users/user/node/app.js:3:13) 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)` – ian Jan 05 '13 at 02:19
0

I believe you are importing a namespace on the first line of app.js. This namespace is make_request. And you are also trying to call a function called makeRequest which is in the namespace make_request. Just try this:

makeRequest.makeRequest("message"); on line 3 and 4 of app.js
Slackware
  • 960
  • 1
  • 13
  • 29
  • Tried but got this: `/Users/user/node/app.js:3 makeRequest.makeRequest("Here's looking at you, kid"); ^ TypeError: Object # has no method 'makeRequest' at Object. (/Users/user/node/app.js:3:13) 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) ` – ian Jan 05 '13 at 02:16
  • Yea maybe I should do some more tutorials before this super basic tutorial? – ian Jan 05 '13 at 03:18