简体   繁体   English

如何在Expressjs / nodejs中启动时调用函数(Setup方法)

[英]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.

相关问题 如何使用Node.js,Express.js和Bluebird调用第二个“外部”函数 - How do you call a second “external” function using Nodejs, Expressjs and bluebird 在 ExpressJS 中,我应该如何从 post 方法中调用异步函数? - In ExpressJS, how should I call an async function from within a post method? 如何配置nodejs / expressjs通过https服务页面? - How do I configure nodejs/expressjs to serve pages over https? 如何使用 nodeJS 在 puppeteer 中调用异步函数之外的函数 - How do I call function outside async function in puppeteer with nodeJS 我如何调用函数并将其放入方法 - how do i call a function and placing it into a method 如何使用 nodejs 启动服务器? - How do I start a server using nodejs? 如何诊断/调试 nodejs/expressjs 的 500 内部服务器错误? - How do I diagnose/debug a 500 internal server error for nodejs/expressjs? 如何限制用户访问ExpressJS / NodeJS中的静态html文件? - How do I restrict the user from accessing the static html files in a ExpressJS/NodeJS? 启动 Web 应用程序时,如何修复 socketio 垃圾邮件轮询? [javascript、expressjs、socketio] - How do I fix socketio spamming polling when I start my web app? [javascript, expressjs, socketio] 如何在函数onclick中调用方法? - How do I call a method within a function onclick?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM