如何在Express服务器启动时运行某个功能? [英] How to run a function when an Express server starts up?

查看:65
本文介绍了如何在Express服务器启动时运行某个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法弄清楚如何在服务器启动时让我的快速服务器运行一个简单的功能。调用函数在服务器启动时运行的适当位置在哪里,以及正确的语法?

I can't seem to figure out how to get my express server to run a simple function when the server starts up. Where is the appropriate place to call a function to run on server startup, and the proper syntax?

我的路由文件中的函数为 exports.myFunction = function(){code here};

I have the function in my routes file as exports.myFunction = function() { code here};

我已经尝试将它作为routes.myFunction粘贴在app.configure块中。我尝试在路由中将其更改为 myfunction(){code} ,然后在配置块中将其作为 routes.myfunction()调用,也没有运气。该函数需要保留在包含我的路由的文件中,因为它会改变那里的一些全局变量。

I've tried sticking it in the app.configure block as routes.myFunction. I've tried changing it in routes to just be myfunction() { code}, then calling it in the configure block as routes.myfunction(), no luck there either. The function needs to stay in the file containing my routes since it alters some global variables there.

我知道这是一些愚蠢的简单语法,但我似乎无法在这里或谷歌上找到任何提示。非常感谢您的帮助!

I know it's some stupidly simple syntax thing, but I can't seem to find any hints here or on google. Much thanks for any help!

推荐答案

使用此活动:

app.on('listening', function () {
    // server ready to accept connections here
});

老实说, app 由快递退回。 createServer()只是http.Server,因此 nodejs docs中描述的所有内容都是如此与http.Server 相关的明文和 railwayjs 有意义。

To be honest app returned by express.createServer() is just http.Server, so everything described in nodejs docs related to http.Server make sense for express and railwayjs.

这篇关于如何在Express服务器启动时运行某个功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆