在Azure Web App上运行Node.js.

Piz*_*Hut 0 azure node.js azure-web-sites koa

我试图在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);
Run Code Online (Sandbox Code Playgroud)

我已经在部署中包含了package.json,因为一些文档建议所需的节点包将被自动安装(Koa等),但它似乎并没有起作用.

有任何想法吗?

Gar*_*SFT 13

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.js或一个app.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>
Run Code Online (Sandbox Code Playgroud)

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

此外,您可以利用VSO在Azure上调试您的应用程序,请参阅Visual Studio 2015和Microsoft Azure同步文件,服务器/本地以获取更多信息.