2017-02-02 51 views
2

將節點.js/express應用程序部署到Azure網站時,我在瀏覽器中收到「Can not GET /」錯誤。相同的應用程序在本地機器上完美運行。Azure網站+ node.js - 無法獲取/

web.config是標準的(我只去除靜態重寫規則),所以:

<!-- All other URLs are mapped to the node.js site entry point --> 
    <rule name="DynamicContent"> 
     <conditions> 
     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> 
     </conditions> 
     <action type="Rewrite" url="src/server/app.js"/> 
    </rule> 
    </rules> 

代碼部署在src/server/(節點功能)和src/client/(靜態內容)文件夾。

按照該FREB記錄在src /服務器/ apps.js是牽強,但最終出現以下錯誤被拋出:

ErrorCode The system cannot find the file specified.  (0x80070002) 

裏面app.js我對靜態文件進行以下配置:

app.use(express.static('./src/client/')); 

回答

1

Azure網站運行package json中定義的文件作爲起始文件所在的文件夾的節點,例如以下:

"start": "node src/server/app.js" 

事實上,它會從src/server文件夾運行node app.js(你可以找到有也iisnode.yml文件)。這會導致所有相對路徑出現混亂。對此的一個解決方案是使用絕對路徑,例如:

app.use(express.static('D:/home/site/wwwroot/src/client/')); 

並且使用例如process.env.NODE_ENV變量。

+0

另一個解決方案是將啓動腳本從src/server /移動到wwwroot /文件夾。這隻需要修改一個文件。 –