[erlang-questions] node.js vs erlang

Tom van Neerijnen tom@REDACTED
Mon Jun 16 16:39:49 CEST 2014


I copy pasted your example and "$ curl http://127.0.0.1:8124/?n=2" =:= 2,
so that works as expected.
However "$ curl http://127.0.0.1:8124/" 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.


On Mon, Jun 16, 2014 at 3:22 PM, Joe Armstrong <erlang@REDACTED> wrote:

> I'm trying to compare node.js with erlang. I'm a total node.js novice BTW
> - but I've written some JS.
>
> Program 1 is just fibonacci - I want a web server to compute fib(N)
>
> So requesting http://127.0.0.1:8124/fib?n=2 should compute and return
> fib(2)
>
> My node.js attempt crashes
> Here's the code in fib.js
>
> --- fib.js
>
> var http = require('http');
> var url  = require('url');
>
> function fib(n) {
>   if (n < 2) {
>     return 1;
>   } else {
>     return fib(n - 2) + fib(n - 1);
>   }
> }
>
> http.createServer(function (req, res) {
>     var q = url.parse(req.url, true).query;
>     var n = q.n;
>     var result = fib(n);
>     console.log('fib('+ n + ')= '+result);
>     res.writeHead(200, {'Content-Type': 'text/plain'});
>     res.end(result.toString());
> }).listen(8124, "127.0.0.1");
>
> console.log('Server running at http://127.0.0.1:8124/');
>
> -- end
>
> -- now we run it
>
> $ node fib.js
> Server running at http://127.0.0.1:8124/
> fib(2)= 2
>
> /home/ejoearm/Dropbox/experiments/hello_world/fib.js:5
> function fib(n) {
>             ^
> RangeError: Maximum call stack size exceeded
>
> fib(2) has run out of stack space????
>
> $ node --version
> v0.8.21
>
> Any ideas what I'm doing wrong
>
> Cheers
>
> /Joe
>
>
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
Thomas van Neerijnen
http://tomvn.com
+4477 1709 7670
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140616/163d1fff/attachment.htm>


More information about the erlang-questions mailing list