问题描述

使用NodeJS的后端应用,开发一个Mobile App的服务端,手机端通过REST API来访问获取后端数据。在本地编译好后,通过npm start启动项目,访问效果如下:

但是,当把项目文件通过FTP,或者直接VS Code 部署到App Service for windows后,访问首页并不是mobile app的页面,而是默认的App Service页面,访问项目里面的API也是404错误?

问题解决

从访问默认URL和测试API为404的效果来看,这是NodeJS项目并没有启动。因为是App Service For Windows环境,非ASP.NET的项目都需要通过IIS启用对于的进程文件来执行代码。如NodeJS项目则会启动一个 node.exe 用于执行 js 文件。

所以第一步是进入到App Service的高级工具 (Kudu)站点中,查看以下图片中的两点:

1: node.exe 进程是否存在

2: node.exe 进程中所加载的js文件是否是正确的启动文件

如果缺少了Web.config文件,则以下面的内容来创建它:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!--
  3. This configuration file is required if iisnode is used to run node processes behind
  4. IIS or IIS Express. For more information, visit:
  5.  
  6. https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
  7. -->
  8.  
  9. <configuration>
  10. <system.webServer>
  11. <!-- 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 -->
  12. <webSocket enabled="false" />
  13. <handlers>
  14. <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
  15. <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
  16. </handlers>
  17.  
  18. <rewrite>
  19. <rules>
  20. <!-- Do not interfere with requests for node-inspector debugging -->
  21. <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
  22. <match url="^app.js\/debug[\/]?" />
  23. </rule>
  24.  
  25. <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
  26. <rule name="StaticContent">
  27. <action type="Rewrite" url="public{REQUEST_URI}"/>
  28. </rule>
  29.  
  30. <!-- All other URLs are mapped to the node.js site entry point -->
  31. <rule name="DynamicContent">
  32. <conditions>
  33. <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
  34. </conditions>
  35. <action type="Rewrite" url="app.js"/>
  36. </rule>
  37. </rules>
  38. </rewrite>
  39.  
  40. <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
  41. <security>
  42. <requestFiltering>
  43. <hiddenSegments>
  44. <remove segment="bin"/>
  45. </hiddenSegments>
  46. </requestFiltering>
  47. </security>
  48.  
  49. <!-- Make sure error responses are left untouched -->
  50. <httpErrors existingResponse="PassThrough" />
  51.  
  52. <!--
  53. You can control how Node is hosted within IIS using the following options:
  54. * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
  55. * node_env: will be propagated to node as NODE_ENV environment variable
  56. * debuggingEnabled - controls whether the built-in debugger is enabled
  57. See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
  58. -->
  59. <!--<iisnode watchedFiles="web.config;*.js"/>-->
  60. </system.webServer>
  61. </configuration>

PS: handlers 和 rewrite 部分是必须的,其中 app.js 的名称可以根据实际项目中的启动文件而修改。

当web.config添加后,问题解决,再次访问效果为:

附录一:如果是遇见的其他错误,如 Module 缺少,则可以在 Logfiles中的 Application 文件夹中的 logging-errors.txt 中查看详细错误,如:

  1. Mon Jan 24 2022 10:59:28 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
  2. Error: Cannot find module 'express'
  3. Require stack:
  4. - D:\home\site\wwwroot\index.js
  5. - D:\Program Files (x86)\iisnode\interceptor.js
  6. at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
  7. at Function.Module._load (internal/modules/cjs/loader.js:667:27)
  8. at Module.require (internal/modules/cjs/loader.js:887:19)
  9. at require (internal/modules/cjs/helpers.js:74:18)
  10. at Object.<anonymous> (D:\home\site\wwwroot\index.js:1:17)
  11. at Module._compile (internal/modules/cjs/loader.js:999:30)
  12. at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
  13. at Module.load (internal/modules/cjs/loader.js:863:32)
  14. at Function.Module._load (internal/modules/cjs/loader.js:708:14)
  15. at Module.require (internal/modules/cjs/loader.js:887:19)
  16. Mon Jan 24 2022 11:09:37 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
  17. Error: Cannot find module 'depd'
  18. Require stack:
  19. - D:\home\site\wwwroot\node_modules\body-parser\index.js
  20. - D:\home\site\wwwroot\node_modules\express\lib\express.js
  21. - D:\home\site\wwwroot\node_modules\express\index.js
  22. - D:\home\site\wwwroot\index.js
  23. - D:\Program Files (x86)\iisnode\interceptor.js
  24. at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
  25. at Function.Module._load (internal/modules/cjs/loader.js:667:27)
  26. at Module.require (internal/modules/cjs/loader.js:887:19)
  27. at require (internal/modules/cjs/helpers.js:74:18)
  28. at Object.<anonymous> (D:\home\site\wwwroot\node_modules\body-parser\index.js:14:17)
  29. at Module._compile (internal/modules/cjs/loader.js:999:30)
  30. at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
  31. at Module.load (internal/modules/cjs/loader.js:863:32)
  32. at Function.Module._load (internal/modules/cjs/loader.js:708:14)
  33. at Module.require (internal/modules/cjs/loader.js:887:19)
  34. Mon Jan 24 2022 11:10:24 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
  35. Error: Cannot find module 'merge-descriptors'
  36. Require stack:
  37. - D:\home\site\wwwroot\node_modules\express\lib\express.js
  38. - D:\home\site\wwwroot\node_modules\express\index.js
  39. - D:\home\site\wwwroot\index.js
  40. - D:\Program Files (x86)\iisnode\interceptor.js
  41. at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
  42. at Function.Module._load (internal/modules/cjs/loader.js:667:27)
  43. at Module.require (internal/modules/cjs/loader.js:887:19)
  44. at require (internal/modules/cjs/helpers.js:74:18)
  45. at Object.<anonymous> (D:\home\site\wwwroot\node_modules\express\lib\express.js:17:13)
  46. at Module._compile (internal/modules/cjs/loader.js:999:30)
  47. at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
  48. at Module.load (internal/modules/cjs/loader.js:863:32)
  49. at Function.Module._load (internal/modules/cjs/loader.js:708:14)
  50. at Module.require (internal/modules/cjs/loader.js:887:19)
  51. Mon Jan 24 2022 11:11:35 GMT+0000 (Greenwich Mean Time): Application has thrown an uncaught exception and is terminated:
  52. Error: Cannot find module 'finalhandler'
  53. Require stack:
  54. - D:\home\site\wwwroot\node_modules\express\lib\application.js
  55. - D:\home\site\wwwroot\node_modules\express\lib\express.js
  56. - D:\home\site\wwwroot\node_modules\express\index.js
  57. - D:\home\site\wwwroot\index.js
  58. - D:\Program Files (x86)\iisnode\interceptor.js
  59. at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
  60. at Function.Module._load (internal/modules/cjs/loader.js:667:27)
  61. at Module.require (internal/modules/cjs/loader.js:887:19)
  62. at require (internal/modules/cjs/helpers.js:74:18)
  63. at Object.<anonymous> (D:\home\site\wwwroot\node_modules\express\lib\application.js:16:20)
  64. at Module._compile (internal/modules/cjs/loader.js:999:30)
  65. at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
  66. at Module.load (internal/modules/cjs/loader.js:863:32)
  67. at Function.Module._load (internal/modules/cjs/loader.js:708:14)
  68. at Module.require (internal/modules/cjs/loader.js:887:19)

然后,直接从本地项目文件夹 node_modules 中的内容 上传到 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

 

【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found -- The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.的更多相关文章

  1. nodejs 开发服务端 部署到 iis 服务器环境 -- iisnode 安装问题解决记录

    开发环境 nodejs: v10.15.3 windows: 10 iis: 10 需求: 用Nodejs开发了服务端,要部署到IIS 需要在IIS服务器上安装iisnode,结果遇到问题:安装不上 ...

  2. 借助Nodejs在服务端使用jQuery采集17173游戏排行信息

    Nodejs相关依赖模块介绍 Nodejs的优势这里就不做介绍啦,这年头相信大家对它也不陌生了.这里主要介绍一下用到的第三方模块. async:js代码中到处都是异步回调,很多时候我们需要做同步处理, ...

  3. 示例 - 25行代码等价实现 - 借助Nodejs在服务端使用jQuery采集17173游戏排行信息

    今天在园子里看到一篇文章: 借助Nodejs在服务端使用jQuery采集17173游戏排行信息 感觉用SS来实现相同功能更加简洁, 于是写了一下, 发现25行代码就搞定了 (包括自动翻页), 于是跟大 ...

  4. nodejs实现服务端重定向

    nodejs实现服务端重定向:https://www.jianshu.com/p/5a1500fcd713

  5. 内网穿透神器(ngrok)服务端部署【分享一台自己的ngrok服务器】【多平台】

    Ngrok为何物 “ngrok 是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.ngrok 可捕获和分析所有通道上的流量,便于后期分析和重放.”这是百度百科上给Ng ...

  6. CAS单点登录之服务端部署

    一.CAS服务端搭建 1.1 CAS支持Http登录配置 CAS默认是要https的链接才能登录的,不过学习的话是可以先去掉https限制,本博客介绍的是基于Cas4.2.7的,之前改过4.0的,详情 ...

  7. Rsync服务端部署流程

    Rsync服务端部署流程       Rsync服务端部署流程: 一.rsync服务端配置流程 配置rsync配置文件/etc/rsyncd.conf 创建同步的本地目录/dingjian 并根据需要 ...

  8. Zabbix5.0服务端部署

    Zabbix5.0服务端部署 基础环境配置 [root@localhost ~]# systemctl disable --now firewalld Removed symlink /etc/sys ...

  9. 【Azure 应用服务】在 App Service for Windows 中自定义 PHP 版本的方法

    问题描述 在App Service for Windows的环境中,当前只提供了PHP 7.4 版本的选择情况下,如何实现自定义PHP Runtime的版本呢? 如 PHP Version 8.1.9 ...

随机推荐

  1. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  2. 【LeetCode】944. Delete Columns to Make Sorted 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 1016 - Brush (II)

    1016 - Brush (II)    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Afte ...

  4. C. Not Equal on a Segment(codeforces)

    C. Not Equal on a Segment time limit per test 1 second memory limit per test 256 megabytes input sta ...

  5. vue基于Blob.js和 Export2Excel.js做前端导出

    1安装三个依赖包 npm install -S file-saver@2.0.2 npm install -S xlsx@0.15.6 npm install -D script-loader@0.7 ...

  6. 使用 JavaScript 中的变量、数据类型和运算符,计算出两个 number 类型的变量与一个 string 类型的变量的和,根据 string 类型处于运算符的不同位置得到不同的结果

    查看本章节 查看作业目录 需求说明: 使用 JavaScript 中的变量.数据类型和运算符,计算出两个 number 类型的变量与一个 string 类型的变量的和,根据 string 类型处于运算 ...

  7. Python_issubclass&isinstance方法和types&inspect内置模块

    issubclass&isinstance issubclass 用于判断一个类是否是一个已知类或是该已知类的子类.注意,该方法只能判断类不能判断实例化对象. class A: pass cl ...

  8. 不同目录存在相同名称的py文件,执行时,报错的解决方法

    1.问题现象如下,执行时报错  imported module 'test_case_execution' has this __file__ attribute platform win32 -- ...

  9. Python中*args 和**kwargs作为形参和实参时的功能详解

    *args 和**kwargs作为形参 *args 和**kwargs作为形参被称为不定长参数,用来处理超出必备参数部分的参数.注意:args和kwargs可以修改为其它变量名. 必备参数就是在定义函 ...

  10. Struts2的jsonp接口实例

    和以往写struts2程序一样,action方法跳转到一个JSP中,为了配合jsonp的跨域,要在JSP中做一个输出 JSP: <%@ page language="java" ...