【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found

简介: 【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found

全文标题:【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.

问题描述

使用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文件,则以下面的内容来创建它:

<?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 的名称可以根据实际项目中的启动文件而修改。

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

 

附录一:如果是遇见的其他错误,如 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 中的内容 上传到 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

 

相关文章
|
3月前
|
安全 Java 应用服务中间件
【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
【Azure 应用服务】App Service 默认页面暴露Tomcat版本信息,存在安全风险
|
8天前
|
JavaScript C++ 容器
【Azure Bot Service】部署NodeJS ChatBot代码到App Service中无法自动启动
2024-11-12T12:22:40.366223350Z Error: Cannot find module 'dotenv' 2024-11-12T12:40:12.538120729Z Error: Cannot find module 'restify' 2024-11-12T12:48:13.348529900Z Error: Cannot find module 'lodash'
32 11
|
11天前
|
JavaScript 前端开发 中间件
JS服务端技术—Node.js知识点
本文介绍了Node.js中的几个重要模块,包括NPM、Buffer、fs模块、path模块、express模块、http模块以及mysql模块。每部分不仅提供了基础概念,还推荐了相关博文供深入学习。特别强调了express模块的使用,包括响应相关函数、中间件、Router和请求体数据解析等内容。文章还讨论了静态资源无法访问的问题及其解决方案,并总结了一些通用设置。适合Node.js初学者参考学习。
27 1
|
16天前
|
开发框架 JavaScript 前端开发
Node.js日记:客户端和服务端介绍、Node.js介绍
Node.js日记:客户端和服务端介绍、Node.js介绍
|
2月前
|
Windows
Windows操作系统部署安装Kerberos客户端
详细介绍了在Windows操作系统上部署安装Kerberos客户端的完整过程,包括下载安装包、安装步骤、自定义安装路径、修改环境变量、配置hosts文件和Kerberos配置文件,以及安装后的验证步骤。
354 3
Windows操作系统部署安装Kerberos客户端
|
2月前
|
JavaScript 应用服务中间件 Linux
宝塔面板部署Vue项目、服务端Node___配置域名
本文介绍了如何使用宝塔面板在阿里云服务器上部署Vue项目和Node服务端项目,并配置域名。文章详细解释了安装宝塔面板、上传项目文件、使用pm2启动Node项目、Vue项目打包上传、以及通过Nginx配置域名和反向代理的步骤。
489 0
宝塔面板部署Vue项目、服务端Node___配置域名
|
3月前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
|
3月前
|
JavaScript Linux
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
|
3月前
|
JavaScript
【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exited with code -1073740791 (0xC0000409) 错误
【Azure Function App】Nodejs Function遇见WorkerProcessExitException : node exited with code -1073740791 (0xC0000409) 错误
|
3月前
|
Docker 容器
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案
【Azure 应用服务】App Service for Container 无法拉取Docker Hub中的镜像替代方案