【问题标题】:node.js on Azure - You do not have permission to view this directory or pageAzure 上的 node.js - 您无权查看此目录或页面
【发布时间】:2022-01-15 21:07:54
【问题描述】:

我在 Azure 上创建了一个应用服务来部署一个 Node.js 应用 (express)。我跟着文档用教程应用程序做了一个zip deploy,它运行良好。

现在我需要部署自己的应用。我用我自己的应用程序的详细信息更新了web.config 文件——app.js 而不是index.js——打包并上传。上传过程报告一切顺利。

但是在点击 url 时出现以下错误:You do not have permission to view this directory or page.

接下来是什么?我该如何调试?

【问题讨论】:

    标签: node.js azure express


    【解决方案1】:

    接下来是什么?

    我遇到了和你一样的错误。你可以参考我的工作步骤。

    app.js

    var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');
    
    var indexRouter = require('./routes/index');
    var usersRouter = require('./routes/users');
    
    var app = express();
    
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'jade');
    
    app.set('port', process.env.PORT || 5000);
    
    console.log("+++++++++++++++"+ app.get('port'));
    
    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    
    app.use('/', indexRouter);
    app.use('/users', usersRouter);
    
    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      next(createError(404));
    });
    
    // error handler
    app.use(function(err, req, res, next) {
      // set locals, only providing error in development
      res.locals.message = err.message;
      res.locals.error = req.app.get('env') === 'development' ? err : {};
    
      // render the error page
      res.status(err.status || 500);
      res.render('error');
    });
    
    module.exports = app;
    
    app.listen(app.get('port'), function(){
              console.log('Express server listening on port ' + app.get('port'));
              });
    

    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>
    

    routes/index.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;
    

    访问结果:

    更多详情,请参考本案例:Running Node.js on Azure Web App。

    如何调试这个?

    要启用调试,请按照以下步骤操作:

    1) 如果不存在,则在根文件夹 (D:\home\site\wwwroot) 中创建文件 iisnode.yml。

    2) 向其中添加以下行。

    loggingEnabled: true
    
    logDirectory: iisnode
    

    完成后,您可以在D:\home\site\wwwroot\iisnode 中找到日志。

    如有任何疑问,请告诉我。

    【讨论】:

    • 两个问题。一,我正在使用winston 来调试我的应用程序。我需要将其更改为您的调试器吗? (我绝对希望避免它,我的代码已经写好了。)第二个问题,我如何到达应用服务机器?来自云壳?还有什么办法吗?
    • @Eddy For One:你仍然可以用你的方式调试你的应用程序,我的调试方式只是让你知道具体的错误细节。二:web 应用服务只是 P-A-A-S 服务,你不能直接访问服务机器。但是,您可以导航到 KUDU url 来检查您的项目。网址是:https://&lt;Your app name&gt;.scm.azurewebsites.net/
    • @Eddy 也许你可以参考这个案例:stackoverflow.com/questions/48390529/azure-default-logs/…
    • 那么解决办法是什么?
    【解决方案2】:

    这个问题的答案在于 package.json 文件—— 在package.json中添加这一行

    "scripts": {
        "start": "node app.js"
    },
    

    然后部署您的应用。

    【讨论】:

      【解决方案3】:

      这个问题一般是缺少web.config引起的。

      对我有用的解决方案:

      最简单的在Linux环境下部署webapp。

      最简单的方法是使用 git 进行部署。在git部署中,git会自动创建一个web.config文件。

      【讨论】:

      【解决方案4】:

      我在 Azure 应用服务中部署 Node.js API 时也遇到了这个问题。

      在 Web 应用 > 设置 > 配置中添加 SCM_DO_BUILD_DURING_DEPLOYMENT 键 并将值设置为 True

      【讨论】:

        【解决方案5】:

        我也遇到过这个问题,这是因为缺少 web.config 文件。 确保通过 Application Settings>SCM_DO_BUILD_DURING_DEPLOYMENT>true 添加应用程序设置 SCM_DO_BUILD_DURING_DEPLOYMENT 参考MS Quickstart for node。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-09-02
          • 2021-01-29
          • 2021-01-07
          • 2018-04-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多