Node.js 运行nodejs web服务器,并显示其主页

Node.js 运行nodejs web服务器,并显示其主页,node.js,browser,Node.js,Browser,我想编写一个node.js脚本来运行web服务器,并且在服务器运行时 启动后,在默认浏览器中打开一个页面。 为了实现后者,我尝试了npm包“open”,它工作得非常完美,除了 本地服务器交付的页面刚刚启动。 脚本类似于: http.createServer(function(request,response){ ... usual stuff }).listen(8001); open('http://www.localhost:8001/index.html'); 我怀疑原因是服务器在

我想编写一个node.js脚本来运行web服务器,并且在服务器运行时 启动后,在默认浏览器中打开一个页面。 为了实现后者,我尝试了npm包“open”,它工作得非常完美,除了 本地服务器交付的页面刚刚启动。 脚本类似于:

http.createServer(function(request,response){
   ... usual stuff
}).listen(8001);
open('http://www.localhost:8001/index.html');
我怀疑原因是服务器在打开时没有实际启动 已执行,但我尝试将其放入计时器,几秒钟后启动,然后
结果相同。

在回调中调用open

比如说

http.createServer(function(request,response){
   ... usual stuff
}).listen(8001,function(){
   open('http://localhost:8001/index.html');
});

在回调函数中调用open

比如说

http.createServer(function(request,response){
   ... usual stuff
}).listen(8001,function(){
   open('http://localhost:8001/index.html');
});

这是因为节点是异步的! open函数在从listen函数得到回调之前被调用! 所以一定要在收到回电后再做!这不像程序编程! 所以使用:
http.createServer(函数(请求、响应){
…平常的事
}).听(8001,函数(){
开放('http://www.localhost:8001/index.html');

});

这是因为节点是异步的! open函数在从listen函数得到回调之前被调用! 所以一定要在收到回电后再做!这不像程序编程! 所以使用:
http.createServer(函数(请求、响应){
…平常的事
}).听(8001,函数(){
开放('http://www.localhost:8001/index.html');
});

已解决。我已从url中删除“www.”。现在最后一个语句是:open(');解决了的。我已从url中删除“www.”。现在最后一个语句是:open(');