当前位置: 首页 > 面试题库 >

在Azure Web App上运行Node.js

章兴发
2023-03-14
问题内容

我试图在Azure Web Appp上运行一个非常简单的node.js服务器以服务单个页面应用程序。服务器将提供静态页面,并且将始终为页面请求提供服务器“
index.html”,因为所有路由均在客户端完成。

所有这些都在本地绝对完美地工作,但是当部署到Azure时,任何页面请求都会导致“您正在寻找的资源已被删除…”,这表明未击中节点服务器。

我正在使用Koa作为服务器,而server.js在这里;

var Koa = require('koa');
var convert = require('koa-convert');
var helmet = require('koa-helmet');
var historyApiFallback = require('koa-connect-history-api-fallback');
var serve = require('koa-static');
var app = new Koa();

// This rewrites all routes requests to the root /index.html file
// (ignoring file requests). If you want to implement isomorphic
// rendering, you'll want to remove this middleware.
app.use(convert(historyApiFallback({
  verbose: false
})));

// Serving ~/dist by default. Ideally these files should be served by
// the web server and not the app server, but this helps to demo the
// server in production.
app.use(convert(serve(__dirname)));
app.use(helmet());

var server = app.listen(3000);var Koa = require('koa');
var convert = require('koa-convert');
var helmet = require('koa-helmet');
var historyApiFallback = require('koa-connect-history-api-fallback');
var serve = require('koa-static');
var app = new Koa();

// This rewrites all routes requests to the root /index.html file
// (ignoring file requests). If you want to implement isomorphic
// rendering, you'll want to remove this middleware.
app.use(convert(historyApiFallback({
  verbose: false
})));

// Serving ~/dist by default. Ideally these files should be served by
// the web server and not the app server, but this helps to demo the
// server in production.
app.use(convert(serve(__dirname)));
app.use(helmet());

var server = app.listen(3000);

我已经将package.json包含在部署中,因为一些文档建议可以自动安装必需的节点程序包(Koa等),但似乎并没有奏效。

有任何想法吗?


问题答案:

Windows
Azure网站使用IISNode在IIS中托管Node进程。实际上,为Node站点分配了一个命名管道,该管道接收传入的请求,而不是像在本地运行或托管自己时使用的TCP端口。即使您可以打开TCP端口,Azure网站实际上也仅适用于传统网站,而无法打开通往外界的端口。

因此,首先,您需要在代码中更改端口,例如: var port = process.env.PORT||3000; //which you can run both on Azure or local var server = app.listen(process.env.PORT||3000);

在我的测试中,我们需要配置一些其他配置,以从应用程序中删除错误,以使其在Azure Web App上运行。

看来这会在https://github.com/alexmingoia/koa-
router/issues/177
直接在Azure上运行koa应用程序时引起同样的问题,因此我们需要配置
nodeProcessCommandLine

创建一个以iisnode.yml内容命名的文件:
nodeProcessCommandLine:"%programfiles%\nodejs\%WEBSITE_NODE_DEFAULT_VERSION%\node.exe" --harmony-generators 设置 WEBSITE_NODE_DEFAULT_VERSION* 相对于您在站点管理器门户网站的“
配置” 标签中的 应用程序设置 中设置的值。
*

作为运行在Azure Web
Apps上的node.js应用程序,需要以server.jsapp.js文件作为根目录中的入口,并带有一个web.config文件来控制iis(如果您通过git部署或通过node.js模板构建应用服务器,它将自动创建)。例如

<configuration>
<system.webServer>

    <handlers>
        <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
        <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
        <rules>
            <!-- Don't interfere with requests for logs -->
            <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
            </rule>

            <!-- Don't interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                <match url="^server.js\/debug[\/]?" />
            </rule>

            <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
            <rule name="StaticContent">
                <action type="Rewrite" url="public{REQUEST_URI}" />
            </rule>

            <!-- All other URLs are mapped to the Node.js application entry point -->
            <rule name="DynamicContent">
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
                </conditions>
                <action type="Rewrite" url="server.js" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

因此,您可以将SPA文件放在public根目录下的文件夹中,例如,您的SPA入口是index.html,当您访问<your_site_name>.azurewebsites.net/index.html它时,它将被重写为“
/public/index.html”。



 类似资料:
  • 问题内容: 提出的每个选项都有一个问题: 我想在普通设备上运行它,所以我希望避免在设备上安装其他操作系统 上个月停止了对JXCore的工作,并且(可能是结果)我无法对其进行编译 自2014年以来就没有开发过NodeJs Anode的Android Port,而且当时并没有真正维护它 我链接到的第二个线程中的第二个答案现在提到了Android的官方节点发行版,但是除了指向最新dist的Browse-

  • 问题内容: 我尝试了以下文档以在Google云上运行node.js应用程序:https : //cloud.google.com/nodejs/getting-started/hello- world Node.js运行正常,但是如果我运行,我会…。 问题答案: 最新的Google Cloud SDK 0.9.65版本存在一个错误。 您可以使用以下命令还原到Cloud SDK 0.9.64: 下一

  • 问题内容: 我正在尝试使用websocket调整我的应用程序以使其在GAE上运行,但是在阅读文档时,我没有找到解决此问题的漂亮方法。 使用一个像这样的非常简单的应用程序:https : //github.com/marcosbergamo/gae-nodejs- websocket 这是我尝试使用的示例演示。但是,当我尝试连接到我的websocket时会收到此错误; 跟随有关请求的图像; 问题答案

  • 问题内容: 借助Android 4.4 KitKat,Google将linux内核升级到3.8版,这是Docker所需的内核版本。 我不知道AUFS部分,但是有没有办法通过此更新在android上运行docker容器? 问题答案: 根据文档,Android内核缺少LXC所需的很多内核功能。 也许将来使用Docker 1.x,可能会编写一个使用Android功能而非LXC的插件。但就目前而言,您需要

  • 我正在使用IntelliJ,我想运行一个以前在Linux中运行的JavaFX项目,我导入了库,并使用了vm参数 <代码>--模块路径/用户/frenk/桌面/javafx-sdk-17/lib--添加模块javafx。控件,javafx。fxml 但我一直收到同样的错误 我看到的所有答案都与vm params有关,所以我无法弄清楚可能是什么问题,我使用java 16作为运行时和macOS bigs

  • 我已经安装了火花图表在我的k8s集群掌舵,我有3个豆荚运行1个主和2个执行,但仍然能够提交火花作业...在“提交应用程序”一节https://github.com/bitnami/charts/tree/master/bitnami/spark中提到我们可以使用。/bin/spark-submit--class org.apache.spark.examples.sparkpi--master s

  • 我试图bin/cassandra-f和我得到以下错误。 xss=-ea-javaagent:bin/./lib/jamm-0.2.5.jar-XX:UseThreadPriorityPolicy-XX:ThreadPriorityPolicy=42-Xms2048M-Xmx2048M-Xmn512M-XX:线程“main”java.lang.NoClassDefFoundError:org/apa