运行 Node.js 在 Azure Web 应用程序上
Running Node.js on Azure Web App
我正在尝试 运行 Azure Web Appp 上的一个非常简单的 node.js 服务器来为单页应用程序提供服务。服务器将提供静态页面,并且将始终服务器 'index.html' 页面请求,因为所有路由都在客户端完成。
一切都在本地完美运行,但是当部署到 Azure 时,任何页面请求都会导致 'the resource you are looking for has been removed...',这表明节点服务器没有受到攻击。
我正在使用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 等),但似乎并没有奏效。
有什么想法吗?
我在上面的代码中看到了重复的代码。删除 var server = app.listen(3000);
后的代码,然后按照文章 https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac 中的步骤完成您的 KOA 到 azure 的 depoly。
Windows Azure 网站使用 IISNode 在 IIS 中托管节点进程。您的节点站点实际上被赋予了一个命名管道,它接收传入的请求,而不是像您在 运行 在本地或托管自己时使用的 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引发相同的问题 运行 koa apps on Azure,所以我们需要配置 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>
因此您可以将 SPA 文件放在根目录的 public
文件夹中,例如你的SPA入口是index.html
,当你访问<your_site_name>.azurewebsites.net/index.html
时它会重写为“/public/index.html”.
此外,您可以利用 VSO 在线调试 Azure 上的应用程序,请参阅 了解更多信息。
我正在尝试 运行 Azure Web Appp 上的一个非常简单的 node.js 服务器来为单页应用程序提供服务。服务器将提供静态页面,并且将始终服务器 'index.html' 页面请求,因为所有路由都在客户端完成。
一切都在本地完美运行,但是当部署到 Azure 时,任何页面请求都会导致 'the resource you are looking for has been removed...',这表明节点服务器没有受到攻击。
我正在使用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 等),但似乎并没有奏效。
有什么想法吗?
我在上面的代码中看到了重复的代码。删除 var server = app.listen(3000);
后的代码,然后按照文章 https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac 中的步骤完成您的 KOA 到 azure 的 depoly。
Windows Azure 网站使用 IISNode 在 IIS 中托管节点进程。您的节点站点实际上被赋予了一个命名管道,它接收传入的请求,而不是像您在 运行 在本地或托管自己时使用的 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引发相同的问题 运行 koa apps on Azure,所以我们需要配置 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>
因此您可以将 SPA 文件放在根目录的 public
文件夹中,例如你的SPA入口是index.html
,当你访问<your_site_name>.azurewebsites.net/index.html
时它会重写为“/public/index.html”.
此外,您可以利用 VSO 在线调试 Azure 上的应用程序,请参阅