在Azure上部署此nodejs应用程序有什么问题?

2hh7jdfx  于 2022-11-03  发布在  Node.js
关注(0)|答案(1)|浏览(178)

bounty将在6天后过期。回答此问题可获得+50的声望奖励。markzzz希望吸引更多人关注此问题。

我已经按照这个教程部署了使用VSCode的nodejs应用程序,Windows:https://learn.microsoft.com/en-us/azure/app-service/quickstart-nodejs?tabs=windows&pivots=development-environment-vscode
不知道为什么,但它“加载”了我应用程序服务上的整个目录,即使使用SCM_DO_BUILD_DURING_DEPLOYMENT参数也是如此:

以下是最后一个远端文件夹(也请注意.vscode文件夹):

当然,它不会加载索引(基本上应该从index.js输出“welcome”):

app.get('/', (req, res) => {
    res.send('welcome!')
})

它只是说 * 您没有权限查看此目录或页面。*
这是我的包裹

{
    "name": "@myapp/backend",
    "version": "1.0.0",
    "description": "",
    "main": "index.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "supervisor index.js",
        "dev": "nodemon ./bin/www"
    },
    "author": "",
    "license": "ISC",
    "dependencies": {
        "bcryptjs": "^2.4.3",
        "body-parser": "^1.19.2",
        "cors": "^2.8.5",
        "crypto": "^1.0.1",
        "dotenv": "^16.0.0",
        "express": "^4.17.3",
        "jsonwebtoken": "^8.5.1",
        "moment": "^2.29.1",
        "mongoose": "^6.2.6",
        "nodemailer": "^6.7.3",
        "nodemailer-sendgrid-transport": "^0.2.0"
    }
}

你怎么了?

x6h2sr28

x6h2sr281#

我已经遵循了提供的相同文档,并且能够将应用程序部署到Azure应用程序服务,没有任何问题。

package.json中,将start更改为

"scripts": {
"start": "node ./bin/www"
},

如给定文档中所述,设置SCM_DO_BUILD_DURING_DEPLOYMENT命令后,Azure应用服务中将生成web.config。但我在您的文件夹结构中没有看到生成的web.config文件。

我的已部署文件夹结构

我的自动生成的web.config文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>  
    <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="bin/www" verb="*" modules="iisnode"/>
    </handlers>
    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^bin/www\/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{PATH_INFO}"/>
        </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="bin/www"/>
        </rule>
      </rules>
    </rewrite>   

    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin"/>
        </hiddenSegments>
      </requestFiltering>
    </security>

    <httpErrors existingResponse="PassThrough" />  
  </system.webServer>
</configuration>

我的package.json

{
    "name": "myexpressapp",
    "version": "0.0.0",
    "private": true,
    "scripts": {
        "start": "node ./bin/www"
    },

"dependencies": {
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "ejs": "~2.6.1",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1"
    }
}

我的索引.js

var  express = require('express');
var  router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title:  'Express' });
});
module.exports = router;

VSCode重新部署应用程序后,请确保SCM_DO_BUILD_DURING_DEPLOYMENT设置在Configuration =〉Application Settings中可用。

已部署的Azure应用程序输出

相关问题