如何知道node.js express服务器何时启动并准备使用 [英] How to know when node.js express server is up and ready to use
问题描述
我在线阅读API,但没有看到这样的东西。确实有一个技巧我不知道可以实现这一点。
如果节点HTTP api(有一个回调并告诉我关于监听事件)是基地对于明确的对象,也许有一个没有记录的express call的回调选项。或者也许我应该知道它在那里。
任何帮助将不胜感激。
Express app.listen 功能 支持回调。它将您传入的参数映射到http.listen调用。
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server,arguments);
};
所以你可以打电话: app.listen(port,callback);
或者您可以直接使用http.listen。
code> var app = require('express')(),
server = require('http')。createServer(app);
server.listen(80,function(){
console.log('ready to go!');
});
Have an application where I want to start a node express server and then start a browser on the same machine automatically as soon as the server is up. How can I query to see if the server is up and ready to go? I really wanted there to be some sort of callback on the .listen call, but doesn't seem to be. I could just wait a longer than I expect amount of time, but this is going on equipment that will be in the field so I either have to wait a ridiculous amount of time to make sure I'm up and running before kicking off the browser or have the user be smart enough to hit refresh if the page doesn't load right. Neither of those are good options for me. . .
I read the API online but don't see anything like this. Surely there's a trick I don't know that can accomplish this.
If the node HTTP api (which has a callback and tells me about the listening event) is the base for the express object, maybe there is a callback option for the express call listen that isn't documented. Or perhaps I'm supposed to just know that it's there.
Any help would be greatly appreciated.
The Express app.listen function does support a callback. It maps the arguments that you pass in to the http.listen call.
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
So you can just call: app.listen(port, callback);
Or you could use http.listen directly.
var app = require('express')(),
server = require('http').createServer(app);
server.listen(80, function() {
console.log('ready to go!');
});
这篇关于如何知道node.js express服务器何时启动并准备使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!