【发布时间】:2012-12-31 16:32:36
【问题描述】:
我在 RingoJS 中遇到了一个非常奇怪的问题。考虑以下保存在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);
运行ringo main.js 会按预期在http://localhost:8080/ 上启动http 服务器,显示默认的notfound 页面。
现在考虑以下代码,除了我将if 条件硬编码为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);
运行ringo main.js 会启动http 服务器,但在打开站点时它会给我一个error 页面而不是notfound 页面。它说 Wrapped java.net.BindException: Address already in use 并给了我以下堆栈跟踪:
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)
发生了什么事? if 条件如何影响服务器?毕竟这两个条件都表示值true(这就是http服务器工作的原因)。这让我非常困惑。是bug吗?
【问题讨论】:
-
传入的请求将在脚本的第二个版本中重新执行您的主模块。我们写“if (module === require.main)”是有原因的,请参阅本页底部:ringojs.org/tutorial/httpserver.md
-
@oberhamsi -
module.exports不是被require记忆并存储在缓存中吗? AFAIK 即使您require同一个模块两次,它也只会执行一次。 -
@oberhamsi - 请您写下您的评论作为答案,以便我接受?
标签: javascript httpserver ringojs