[英]How do I call a function at start in Expressjs/nodejs (Setup method)
I want my worker server to pickup tasks from parent server immediately after it starts and keep pinging for pending tasks. 我希望我的工作服务器在启动后立即从父服务器获取任务,并继续ping待处理的任务。
Given that Nodejs is a event driven, how do I make my express server make a rest call to server and start working immediately after start? 鉴于Nodejs是一个事件驱动的,如何让我的快速服务器对服务器进行休息调用并在启动后立即开始工作?
In a way, I am asking for setup method in Nodejs. 在某种程度上,我在Nodejs中要求设置方法。
You have your server.js
file (the one you call node server.js
on). 您有
server.js
文件(您调用node server.js
文件)。
Simply place whatever code you need to run at bootstrap there. 只需放置在引导程序中运行所需的任何代码。
var express = require("express");
...
yourSetupFunction();
app.listen(1337);
I'm not sure what type of tasks you fetch from parent node. 我不确定从父节点获取什么类型的任务。 So I'm assuming you just want to know when the new server is ready to use and perform actions based on that.
所以我假设您只想知道新服务器何时可以使用并基于此执行操作。
The express documentation mentions about app.listen as a function that can be configured. 快递文档提到app.listen是一个可以配置的功能。
Here's the code block 这是代码块
app.listen = function(){
var server = http.createServer(this);
//yourBootstrap();
return server.listen.apply(server, arguments);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.