FloIancu
FloIancu

Reputation: 2725

NodeJS function not called

I am calling a function to build a specially formatted string (x2) from an array of strings (x).

Code:

http.createServer(function handler(req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    ...
    function test(x){
        var x2="";
        x2.concat(x);
        return x2;
    }
    ...
    var x = "abcd";
    console.log(test(x));
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

The result is that the function doesn't get called. I've tried placing a console.log("...") inside and it doesn't show either. Everything else in the project works fine.

I'm completely new to JS/NodeJS so it might be something trivial I'm missing.

Upvotes: 1

Views: 1594

Answers (2)

Randy
Randy

Reputation: 4401

What you're probably looking for is to return the result of concat because concat doesn't affect the either String and instead returns a new String.

return x2.concat(x);

See this link for information about concat.

Also, I'd take a look at the "Performance" section of that link. It says:

It is strongly recommended that assignment operators (+, +=) are used instead of the concat() method.

Upvotes: 2

officert
officert

Reputation: 1232

Based on your question I'm not sure if you are getting an error or what exactly is happening. From looking at your code everything looks like it should work okay, but I'm guessing your request never finishes. Try adding

res.end('okay');

to your handler function. This will close the request and send back 'okay' as 'text/html'.

Upvotes: 0

Related Questions