如何知道 node.js express 服务器何时启动并可以使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18241494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:19:00  来源:igfitidea点击:

How to know when node.js express server is up and ready to use

javascriptnode.jsexpress

提问by Brian

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. . .

有一个应用程序,我想在其中启动节点快速服务器,然后在服务器启动后立即在同一台机器上自动启动浏览器。如何查询服务器是否已启动并准备就绪?我真的希望在 .listen 调用中有某种回调,但似乎没有。我可以等待比我预期的更长的时间,但这是在现场的设备上进行的,所以我要么必须等待一段荒谬的时间,以确保在启动浏览器之前我已经启动并运行或者让用户足够聪明以在页面加载不正确时点击刷新。这些对我来说都不是很好的选择。. .

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.

我在线阅读了 API,但没有看到类似的内容。当然有一个我不知道的技巧可以做到这一点。

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.

如果节点 HTTP api(它有一个回调并告诉我有关侦听事件的信息)是 express 对象的基础,则可能有一个未记录的用于 express 调用侦听的回调选项。或者也许我应该只知道它在那里。

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Timothy Strimple

The Express app.listenfunction doessupport a callback. It maps the arguments that you pass in to the http.listen call.

Express app.listen函数确实支持回调。它将您传入的参数映射到 http.listen 调用。

app.listen = function(){
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

So you can just call: app.listen(port, callback);

所以你可以打电话: app.listen(port, callback);

Or you could use http.listen directly.

或者你可以直接使用 http.listen。

var app = require('express')(),
    server = require('http').createServer(app);

server.listen(80, function() {
    console.log('ready to go!');
});

回答by diosney

You can use the http.listenmethod which has a callback function that triggers once the server is ready:

您可以使用http.listen具有回调函数的方法,该函数在服务器准备就绪后触发:

http.createServer(app).listen(app.get('port'), function () {
    console.log('Printed when ready!!!');
});

See the official reference at Node.js:

请参阅 Node.js 上的官方参考:

http://nodejs.org/api/all.html#all_server_listen_port_hostname_backlog_callback

http://nodejs.org/api/all.html#all_server_listen_port_hostname_backlog_callback

http://nodejs.org/api/all.html#all_server_listen_path_callback_1

http://nodejs.org/api/all.html#all_server_listen_path_callback_1

http://nodejs.org/api/all.html#all_server_listen_handle_callback_1

http://nodejs.org/api/all.html#all_server_listen_handle_callback_1

回答by Peter Lyons

As many have mentioned, the listenfunction (on the express app or an http server, both support it), does support a callback and that will let your node process know when it is listening.

正如许多人提到的,该listen函数(在 express 应用程序或 http 服务器上,都支持它)确实支持回调,这会让您的节点进程知道它何时正在侦听。

So if you plan to launch the browser from within your express app, do it there and you are good. However, if you are launching the express app from an external script and then want that external script to open the browser, the node callback doesn't really buy you anything.

因此,如果您打算从 Express 应用程序中启动浏览器,请在那里执行,您会很好。但是,如果您从外部脚本启动 express 应用程序,然后希望该外部脚本打开浏览器,则节点回调实际上并没有给您带来任何好处。

Waiting for some magic string on stdout isn't really an improvement on just waiting for a good HTTP response. You may as well just use a try/backoff/timeout loop with curluntil you get a successful response.

在标准输出上等待一些神奇的字符串并不是仅仅等待一个好的 HTTP 响应的改进。您也可以使用 try/backoff/timeout 循环,curl直到获得成功响应。

回答by Gergo

You can fire a custom event after the server is started:

您可以在服务器启动后触发自定义事件:

// server.js
const express = require('express');
const app = express();

modeule.export = app;
app.listen(3000, () => {
   app.emit('listened', null)
});

In a separate module, the app can listen your custom event:

在单独的模块中,应用程序可以监听您的自定义事件:

// custom.js
const server = require('server.js');
server.on('listened', function() {
  console.log('The server is running!');
});

回答by P.Brian.Mackey

server.on('listening', function() {
  resolve();//I added my own promise to help me await
});

The Listening eventworked for me. Note I added my own Promise. I imagine you could obtain similar results without a promise by adding an entry point to this listener.

聆听事件对我有用。注意我添加了我自己的 Promise。我想您可以通过向此侦听器添加入口点而无需承诺即可获得类似的结果。

Note, I tried the more intuitive server.on('listen')and it didn't work. Running node 6.9.1

请注意,我尝试了更直观的方法server.on('listen'),但没有奏效。运行节点 6.9.1