【Azure 應(yīng)用服務(wù)】Azure Mobile App (NodeJS) 的服務(wù)端部署在App Service for Windows中出現(xiàn)404 Not Found

問題描述

使用NodeJS的后端應(yīng)用鳖昌,開發(fā)一個Mobile App的服務(wù)端,手機端通過REST API來訪問獲取后端數(shù)據(jù)湿诊。在本地編譯好后矫膨,通過npm start啟動項目差凹,訪問效果如下:


image

但是,當把項目文件通過FTP侧馅,或者直接VS Code 部署到App Service for windows后危尿,訪問首頁并不是mobile app的頁面,而是默認的App Service頁面馁痴,訪問項目里面的API也是404錯誤谊娇?


image

問題解決

從訪問默認URL和測試API為404的效果來看,這是NodeJS項目并沒有啟動罗晕。因為是App Service For Windows環(huán)境济欢,非ASP.NET的項目都需要通過IIS啟用對于的進程文件來執(zhí)行代碼。如NodeJS項目則會啟動一個 node.exe 用于執(zhí)行 js 文件小渊。

所以第一步是進入到App Service的高級工具 (Kudu)站點中法褥,查看以下圖片中的兩點:

1: node.exe 進程是否存在

2: node.exe 進程中所加載的js文件是否是正確的啟動文件


image

如果缺少了Web.config文件,則以下面的內(nèi)容來創(chuàng)建它:

<?xml version="1.0" encoding="utf-8"?>
<!-- This configuration file is required if iisnode is used to run node processes behind
     IIS or IIS Express.  For more information, visit:

     https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config -->

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <webSocket enabled="false" />
    <handlers>
      <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
      <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
    </handlers>

       <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^app.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 site entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
          </conditions>
          <action type="Rewrite" url="app.js"/>
        </rule>
      </rules>
    </rewrite>

    <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <!-- Make sure error responses are left untouched -->
    <httpErrors existingResponse="PassThrough" />

    <!-- You can control how Node is hosted within IIS using the following options:
        * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
        * node_env: will be propagated to node as NODE_ENV environment variable
        * debuggingEnabled - controls whether the built-in debugger is enabled
      See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options -->
    <!--<iisnode watchedFiles="web.config;*.js"/>-->
  </system.webServer>
</configuration>

**PS: **handlers 和 rewrite 部分是必須的酬屉,其中 app.js 的名稱可以根據(jù)實際項目中的啟動文件而修改半等。

當web.config添加后揍愁,問題解決,再次訪問效果為:

image

附錄一:如果是遇見的其他錯誤杀饵,如 Module 缺少莽囤,則可以在 Logfiles中的 Application 文件夾中的 logging-errors.txt 中查看詳細錯誤,如:

Mon Jan 24 2022 10:59:28 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'express'
Require stack:
- D:\home\site\wwwroot\index.js
- D:\Program Files (x86)\iisnode\interceptor.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (D:\home\site\wwwroot\index.js:1:17)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
Mon Jan 24 2022 11:09:37 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'depd'
Require stack:
- D:\home\site\wwwroot\node_modules\body-parser\index.js
- D:\home\site\wwwroot\node_modules\express\lib\express.js
- D:\home\site\wwwroot\node_modules\express\index.js
- D:\home\site\wwwroot\index.js
- D:\Program Files (x86)\iisnode\interceptor.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (D:\home\site\wwwroot\node_modules\body-parser\index.js:14:17)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
Mon Jan 24 2022 11:10:24 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'merge-descriptors'
Require stack:
- D:\home\site\wwwroot\node_modules\express\lib\express.js
- D:\home\site\wwwroot\node_modules\express\index.js
- D:\home\site\wwwroot\index.js
- D:\Program Files (x86)\iisnode\interceptor.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (D:\home\site\wwwroot\node_modules\express\lib\express.js:17:13)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
Mon Jan 24 2022 11:11:35 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
Error: Cannot find module 'finalhandler'
Require stack:
- D:\home\site\wwwroot\node_modules\express\lib\application.js
- D:\home\site\wwwroot\node_modules\express\lib\express.js
- D:\home\site\wwwroot\node_modules\express\index.js
- D:\home\site\wwwroot\index.js
- D:\Program Files (x86)\iisnode\interceptor.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (D:\home\site\wwwroot\node_modules\express\lib\application.js:16:20)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)

然后切距,直接從本地項目文件夾 node_modules 中的內(nèi)容 上傳到 App Service 中朽缎,即可解決 connot find module ‘ **** ’ 錯誤。

附錄二:示例代碼

下載地址:https://files.cnblogs.com/files/lulight/nodejs-express-azuermobileapp-web.confg.zip?t=1643031258

參考資料

Create a Node.js web app in Azure:https://docs.microsoft.com/en-us/azure/app-service/quickstart-nodejs?tabs=windows&pivots=development-environment-vscode

Node.js Hello World in App Service: https://github.com/Azure-Samples/nodejs-docs-hello-world

當在復(fù)雜的環(huán)境中面臨問題谜悟,格物之道需:濁而靜之徐清话肖,安以動之徐生。 云中赌躺,恰是如此!

分類: 【Azure 應(yīng)用服務(wù)】

標簽: App Service, nodejs web.config, NodeJS + Express, 解決node.js的404問題

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末狼牺,一起剝皮案震驚了整個濱河市羡儿,隨后出現(xiàn)的幾起案子礼患,更是在濱河造成了極大的恐慌,老刑警劉巖掠归,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缅叠,死亡現(xiàn)場離奇詭異,居然都是意外死亡虏冻,警方通過查閱死者的電腦和手機肤粱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來厨相,“玉大人领曼,你說我怎么就攤上這事÷” “怎么了庶骄?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長践磅。 經(jīng)常有香客問我单刁,道長,這世上最難降的妖魔是什么府适? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任羔飞,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘乘综。我一直安慰自己跳纳,他們只是感情好,可當我...
    茶點故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布卡儒。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪朋贬。 梳的紋絲不亂的頭發(fā)上凯楔,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天,我揣著相機與錄音锦募,去河邊找鬼摆屯。 笑死,一個胖子當著我的面吹牛糠亩,可吹牛的內(nèi)容都是我干的虐骑。 我是一名探鬼主播,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼赎线,長吁一口氣:“原來是場噩夢啊……” “哼廷没!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起垂寥,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤颠黎,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后滞项,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體狭归,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年文判,在試婚紗的時候發(fā)現(xiàn)自己被綠了过椎。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,768評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡戏仓,死狀恐怖疚宇,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赏殃,我是刑警寧澤敷待,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站嗓奢,受9級特大地震影響讼撒,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜股耽,卻給世界環(huán)境...
    茶點故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一根盒、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧物蝙,春花似錦炎滞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽钠导。三九已至,卻和暖如春森瘪,著一層夾襖步出監(jiān)牢的瞬間牡属,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工扼睬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留逮栅,地道東北人。 一個月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓窗宇,卻偏偏與公主長得像措伐,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子军俊,可洞房花燭夜當晚...
    茶點故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容