<div dir="ltr">I copy pasted your example and "$ curl <a href="http://127.0.0.1:8124/?n=2">http://127.0.0.1:8124/?n=2</a>" =:= 2, so that works as expected.<br>However "$ curl <a href="http://127.0.0.1:8124/">http://127.0.0.1:8124/</a>" resulted in the same crash as you got. Put a console.log(q); and maybe a console.log(n); in your http.createServer callback to make sure n is what you expect it to be. My guess is it's undefined or something that isn't a number, which your base case will never match and cause your call stack to overflow.<br>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jun 16, 2014 at 3:22 PM, 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><br clear="all"><br>-- <br><div dir="ltr"><div>Thomas van Neerijnen<br></div><a href="http://tomvn.com" target="_blank">http://tomvn.com</a><br><div>+4477 1709 7670</div></div>
</div>