在 Azure Web 应用程序上运行 Node.js [英] Running Node.js on Azure Web App

查看:45
本文介绍了在 Azure Web 应用程序上运行 Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一切都在本地完美运行,但是在部署到 Azure 时,任何页面请求都会导致您正在寻找的资源已被删除...",这表明节点服务器没有被命中.

我用的是Koa作为服务器,server.js在这里;

var Koa = require('koa');var convert = require('koa-convert');var helm = require('koa-helmet');var historyApiFallback = require('koa-connect-history-api-fallback');var serve = require('koa-static');var app = new Koa();//这会将所有路由请求重写到根/index.html 文件//(忽略文件请求).如果你想实现同构//渲染,您需要删除此中间件.app.use(convert(historyApiFallback({详细:错误})));//默认为 ~/dist 服务.理想情况下,这些文件应该由//Web 服务器而不是应用程序服务器,但这有助于演示//生产中的服务器.app.use(convert(serve(__dirname)));app.use(头盔());var server = app.listen(3000);var Koa = require('koa');var convert = require('koa-convert');var helm = require('koa-helmet');var historyApiFallback = require('koa-connect-history-api-fallback');var serve = require('koa-static');var app = new Koa();//这会将所有路由请求重写到根/index.html 文件//(忽略文件请求).如果你想实现同构//渲染,您需要删除此中间件.app.use(convert(historyApiFallback({详细:错误})));//默认为 ~/dist 服务.理想情况下,这些文件应该由//Web 服务器而不是应用程序服务器,但这有助于演示//生产中的服务器.app.use(convert(serve(__dirname)));app.use(头盔());var server = app.listen(3000);

我已将 package.json 包含在部署中,因为某些文档建议将自动安装所需的节点包(Koa 等),但似乎没有奏效.

有什么想法吗?

解决方案

Windows Azure 网站使用 IISNode 在 IIS 内托管 Node 进程.您的 Node 站点实际上有一个 Named Pipe 来接收传入的请求,而不是像您在本地运行或自己托管时使用的 TCP 端口.即使可以打开 TCP 端口,Azure 网站实际上也仅适用于传统网站,无法向外界开放端口.

因此,首先,您需要更改代码中的端口,例如:<代码>var port = process.env.PORT||3000;//您可以在 Azure 或本地运行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% odejs\%WEBSITE_NODE_DEFAULT_VERSION% ode.exe" --harmony-generators设置 WEBSITE_NODE_DEFAULT_VERSION 与您在站点管理器门户的配置标签中的应用设置中设置的值相关.

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

<预><代码><配置><system.webServer><处理程序><!--表示app.js文件是iisnode模块处理的node.js应用--><add name="iisnode" path="server.js" verb="*" modules="iisnode"/></处理程序><重写><规则><!-- 不要干扰对日志的请求--><rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true"><match url="^[a-zA-Z0-9_-]+.js.logs/d+.txt$"/></规则><!-- 不要干扰节点检查器调试的请求--><rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"><match url="^server.js/debug[/]?"/></规则><!-- 首先我们考虑传入的 URL 是否匹配/public 文件夹中的物理文件 --><规则名称="静态内容"><action type="Rewrite" url="public{REQUEST_URI}"/></规则><!-- 所有其他 URL 都映射到 Node.js 应用程序入口点 --><规则名称="动态内容"><条件><add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/></条件><action type="重写" url="server.js"/></规则></规则></rewrite></system.webServer>

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

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

I am trying to run a very simple node.js server on an Azure Web Appp to serve a single page application. The server will serve up static pages, and will always server 'index.html' for page requests as all the routing is done client side.

All works absolutely perfectly locally, but when deploying to Azure, any page requests result in a 'the resource you are looking for has been removed...', which suggests the node server isn't being hit.

I am using Koa as the server, and the server.js is here;

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);

I have included the package.json on the deployment as some documentation suggested that required node packages would be auto-installed (Koa etc), but it doesnt seem as though that has worked.

Any ideas?

解决方案

Windows Azure Websites uses IISNode to host the Node process inside of IIS. Your Node site is actually given a Named Pipe which receives the incoming requests, not a TCP port like you would use when running locally or hosting yourself. Even if you could open a TCP port, Azure Websites are really only meant for traditional websites and don't have a way to open ports to the outside world.

So, first, you need to change the port in your code, e.g.: var port = process.env.PORT||3000; //which you can run both on Azure or local var server = app.listen(process.env.PORT||3000);

And in my test, there are several additional configurations we need to configure to remove errors from application to make it run on Azure Web App.

It seems it will raise the same issue with https://github.com/alexmingoia/koa-router/issues/177 directly run koa apps on Azure, so we need to configure the nodeProcessCommandLine:

create a the a file named iisnode.yml with the content: nodeProcessCommandLine:"%programfiles% odejs\%WEBSITE_NODE_DEFAULT_VERSION% ode.exe" --harmony-generators the setting WEBSITE_NODE_DEFAULT_VERSION is relative to the value you set in app settings in configure tab of in your site manager portal.

As a node.js application running on Azure Web Apps, needs a server.js or app.js file as the entrance in the root directory with a web.config file to control the iis (it will automatically create if you deploy via git or build the app server via node.js template). E.G.

<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>

So you can put your SPA files in the public folder in root directory, e.g. your SPA entrance is index.html, when you visit <your_site_name>.azurewebsites.net/index.html it will rewrite to "/public/index.html".

Additionally, you can leverage VSO to debug your application on Azure online, refer to Visual Studio 2015 and Microsoft Azure syncing files, Server/Local for more.

这篇关于在 Azure Web 应用程序上运行 Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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