在Azure Web App上运行Node.js [英] Running Node.js on Azure Web App

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

问题描述

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

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.

所有操作在本地都绝对完美,但是在部署到Azure时,任何页面请求都会导致您正在寻找的资源已被删除...",这表明节点服务器未受到攻击.

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.

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

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

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

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.

有什么想法吗?

推荐答案

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

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.

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

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

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

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.

似乎它将与 https://github.com/alexmingoia提出同样的问题/koa-router/issues/177 直接在Azure上运行koa应用程序,因此我们需要配置 nodeProcessCommandLine :

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:

创建一个名为iisnode.yml的文件,其内容为: nodeProcessCommandLine:"%programfiles%\nodejs\%WEBSITE_NODE_DEFAULT_VERSION%\node.exe" --harmony-generators 设置 WEBSITE_NODE_DEFAULT_VERSION 相对于您在站点管理器门户的配置标签中的应用设置中设置的值.

create a the a file named iisnode.yml with the content: nodeProcessCommandLine:"%programfiles%\nodejs\%WEBSITE_NODE_DEFAULT_VERSION%\node.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.

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

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>

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

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

此外,您可以利用VSO在线在Azure上调试应用程序,请参考

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 App上运行Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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