2016-03-08 111 views
0

我想在Azure Web Appp上運行一個非常簡單的node.js服務器來爲單個頁面應用程序提供服務。服務器將提供靜態頁面,並且始終爲頁面請求服務器'index.html',因爲所有路由都在客戶端完成。在Azure Web App上運行Node.js

所有工作絕對完美,但在部署到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等),但它不看起來好像已經奏效。

任何想法?

+0

我可以在上面的代碼中看到重複的代碼。 'var server = app.listen(3000);'後刪除代碼並再次嘗試 –

+0

道歉,我明顯粘貼兩次,因此重複(這是一個深夜)。不幸的是我不能編輯這個帖子,但是實際的版本沒有重複。 – PizzaTheHut

+0

我已經將KOA應用部署到天藍色,沒有任何錯誤。我認爲你可以參考文章https://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac將你的代碼移動到git,然後將其移動到天藍。記得要配置好你的package.json。我通過cmd'npm install koa'安裝koa並將其寫入package.json,如下所示:「dependencies」:{ 「express」:「3.2.6」, 「jade」:「*」, 「koa」: 「*」 }。最好使用'app.listen(process.env.PORT || 3000);'在你的app.js中替換'app.listen(3000);'。 –

回答

9

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直接運行在Azure上KOA的應用程序,所以我們需要配置nodeProcessCommandLine

創造了與內容命名iisnode.yml文件: nodeProcessCommandLine:"%programfiles%\nodejs\%WEBSITE_NODE_DEFAULT_VERSION%\node.exe" --harmony-generators 的設置爲WEBSITE_NODE_DEFAULT_VERSION與您在中設置的值相關配置您的網站管理器門戶中的選項卡。

隨着Azure上的Web應用程序運行的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 and Microsoft Azure syncing files, Server/Local更多。