ian
ian

Reputation: 12335

what is wrong with my simple node.js function?

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;

Upvotes: 0

Views: 1526

Answers (3)

Nix
Nix

Reputation: 58522

You need to call the function.

makeRequest.makeRequest("Your message");

You can also simplify your code by doing:

module.exports.makeRequest = function(message) {
    //... 
};

Upvotes: 1

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123453

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.

Upvotes: 4

Slackware
Slackware

Reputation: 1000

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

Upvotes: 0

Related Questions