簡體   English   中英

在Azure Web App上運行Node.js

[英]Running Node.js on Azure Web App

我試圖在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); 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相對於您在configure中的應用程序設置中設置的值您的站點管理器門戶中的標簽。

作為運行在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” 。

此外,您可以利用VSO在Azure在線上調試應用程序,有關更多信息,請參考Visual Studio 2015和Microsoft Azure同步文件(服務器/本地)

我可以在上面的代碼中看到重復的代碼。 var server = app.listen(3000);之后刪除代碼 然后按照文章https://azure.microsoft.com/zh-CN/documentation/articles/web-sites-nodejs-develop-deploy-mac中的步驟操作,以完成將您的KOA分解為天藍色的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM