<div dir="ltr">Hello, Joe.<div><br></div><div>If you include a console.log(req.url); inside your createServer callback you'll probably see that your browser is trying to obtain /favicon.ico (a second request), which causes the infinite loop, thus the RangeError, as n is undefined for the second call and you aren't dealing with this condition.</div>
<div><br></div><div>Hope it helps.</div><div><br></div><div>- Paulo F. Oliveira</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 16 June 2014 15:22, Joe Armstrong <span dir="ltr"><<a href="mailto:erlang@gmail.com" target="_blank">erlang@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I'm trying to compare node.js with erlang. I'm a total node.js novice BTW - but I've written some JS.</div>
<div><br></div><div>Program 1 is just fibonacci - I want a web server to compute fib(N)</div>
<div><br></div><div>So requesting <a href="http://127.0.0.1:8124/fib?n=2" target="_blank">http://127.0.0.1:8124/fib?n=2</a> should compute and return fib(2)</div><div><br></div><div>My node.js attempt crashes</div><div>Here's the code in fib.js</div>

<div><br></div><div>--- fib.js</div><div><br></div><div><div>var http = require('http');</div><div>var url  = require('url');</div><div><br></div><div>function fib(n) {</div><div>  if (n < 2) {</div><div>

    return 1;</div><div>  } else {</div><div>    return fib(n - 2) + fib(n - 1);</div><div>  }</div><div>}</div><div><br></div><div>http.createServer(function (req, res) {</div><div>    var q = url.parse(req.url, true).query;</div>

<div>    var n = q.n;</div><div>    var result = fib(n);</div><div>    console.log('fib('+ n + ')= '+result);</div><div>    res.writeHead(200, {'Content-Type': 'text/plain'});</div><div>    res.end(result.toString());</div>

<div>}).listen(8124, "127.0.0.1");</div><div><br></div><div>console.log('Server running at <a href="http://127.0.0.1:8124/'" target="_blank">http://127.0.0.1:8124/'</a>);</div></div><div><br></div><div>
-- end</div><div>
<br></div><div>-- now we run it</div><div><div><br></div><div>$ node fib.js</div><div>Server running at <a href="http://127.0.0.1:8124/" target="_blank">http://127.0.0.1:8124/</a></div><div>fib(2)= 2</div><div><br></div>
<div>/home/ejoearm/Dropbox/experiments/hello_world/fib.js:5</div>
<div>function fib(n) {</div><div>            ^</div><div>RangeError: Maximum call stack size exceeded</div></div><div><br></div><div>fib(2) has run out of stack space????</div><div><div><br></div><div>$ node --version</div>

<div>v0.8.21</div></div><div><br></div><div>Any ideas what I'm doing wrong</div><div><br></div><div>Cheers</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>/Joe</div><div><br></div><div><br></div><div>
<br></div><div><br></div><div><br></div>
<div><br></div></font></span></div>
<br>_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br></blockquote></div><br></div>