简体   繁体   English

在Azure的iisnode上运行的if(require.main)模式

[英]Pattern for if(require.main) that works on Azure's iisnode

A pattern seen in both Node and Python development that allows a module to be used both standalone and as a module looks like this: 在Node和Python开发中都可以看到的一种模式它允许模块既可以独立使用也可以作为模块使用,如下所示:

function main() {
    // ....
}

if(require.main === module) {
    main();
}

module.exports = {
    start : main
}

Unfortunately, this does not work on Microsoft's Azure cloud service, as iisnode overwrites require.main with something else (not sure what). 不幸的是,这不起作用在微软的Azure云服务,如iisnode覆盖require.main用别的东西 (不知道)。 How can I rewrite the check to work on Azure as well, starting the main module if it is the main entry point? 如果主检查点是主模块,我如何重写该检查使其也能在Azure上工作?

I only found a hackish solution to this problem, which basically detects whether I am running iisnode or not: 我只找到了针对该问题的简单解决方案,该解决方案基本上可以检测到我是否正在运行iisnode:

var onIISNode = require.main.filename.match(/iisnode/);
if(onIISNode || require.main === module){ ... }

Improvements are welcome ... especially ones actually using the official Azure API for NodeJS 欢迎进行改进...特别是实际上使用官方的NodeJS Azure API进行的改进

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM