<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">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/'">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/">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><div><br></div><div>/Joe</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>
<div><br></div></div>