Question

I've encountered a very weird problem in RingoJS. Consider the following code saved in main.js:

var {Application} = require("stick");
var {main} = require("ringo/httpserver");
var app = exports.app = new Application;
app.configure("error", "notfound");
if (module === require.main)
    main(module.id);

Running ringo main.js starts the http server on http://localhost:8080/ as expected, displaying the default notfound page.

Now consider the following code, which is the same as above, except that I hard coded the if condition as true:

var {Application} = require("stick");
var {main} = require("ringo/httpserver");
var app = exports.app = new Application;
app.configure("error", "notfound");
if (true) main(module.id);

Running ringo main.js starts the http server, but on opening the site it gives me an error page instead of the notfound page. It says Wrapped java.net.BindException: Address already in use and gives me the following stack trace:

at ringo/httpserver.js:327 (Server)
at ringo/httpserver.js:428 (init)
at ringo/httpserver.js:506 (main)
at /home/aaditmshah/main.js:5
at ringo/jsgi/connector.js:28 (handleRequest)

What's happening? How does the if condition affect the server? After all both the conditions express the value true (which is the reason why the http server works). This has me abosultely confounded. Is it a bug?

Was it helpful?

Solution

the request coming in will re-execute your main module in the second version of your script. there's a reason why we write "if (module === require.main)", see the bottom of this page: http://ringojs.org/tutorial/httpserver.md

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top