【问题标题】:Disable directory browsing in Nodejs, Azure在 Nodejs、Azure 中禁用目录浏览
【发布时间】:2021-04-06 00:46:27
【问题描述】:

我正在尝试禁用在 Azure 上托管的节点中的目录浏览。

这是我的根文件夹,我想禁用浏览。

以下是我的 index.js 文件

const express = require('express');

const publicweb = './';
const app = express();

app.use(express.static(publicweb));
app.disable('x-powered-by');
console.log(`serving ${publicweb}`);

app.get('*', (req, res) => {
  res.sendFile(`index.html`, { root: publicweb });
});

const port = process.env.PORT || '3500';
app.listen(port, () => console.log(`API running on localhost:${port}`));

我不希望前端应用程序可以访问这些文件。

【问题讨论】:

  • 节点没有目录浏览。如果任何浏览器请求文件/url,express.static 会检查该文件是否存在于硬盘/文件系统上,并读取其内容并将其发送回客户端。如果文件不存在,则返回 404(未找到)状态。使用您的“catch all”路线(不确定是否 100%),您为每个未找到“index.html”文件内容的文件提供服务。
  • 感谢@Marc 让我朝这个方向思考。所以这里的问题是我的部署文件列在与express.static 相同的目录中,这就是为什么像index.js 这样的文件可以在浏览器中访问。你能建议我在这种情况下应该怎么做吗?
  • 我注意到您的“publicweb”与节点的“当前工作目录”相同。将您的静态/资产文件放在“公共”或您喜欢的子文件夹中,并仅通过 express.static 提供子文件夹。但是,有可能在 azure 上正在运行其他网络服务器,这会将“wwwroot”暴露给所有人,并且您会得到相同的结果。我对 Azure 没有任何经验,但我 100% 肯定,有一个解决方案可以禁用目录侦听。站点说明:避免通过节点提供静态文件并使用专用的 http 服务器,仅将节点用于“API/后端”

标签: node.js azure directory azureportal


【解决方案1】:

您可以尝试创建一个 web.config,在后台 Azure 使用 IIS 为您的应用程序提供服务

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <system.webServer>
       <webSocket enabled="false" />
          <handlers>
                <add name="iisnode" path="app.js" verb="*" modules="iisnode"/>
          </handlers>
          <rewrite>
               <rules>
                    <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                        <match url="^app.js\/debug[\/]?" />
                    </rule>
                    <rule name="StaticContent">
                         <action type="Rewrite" url="public{REQUEST_URI}"/>
                    </rule>
                    <rule name="DynamicContent">
                         <conditions>
                              <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
                         </conditions>
                         <action type="Rewrite" url="app.js"/>
                    </rule>
               </rules>
          </rewrite>
          <security>
               <requestFiltering>
                    <hiddenSegments>
                         <remove segment="bin"/>
                    </hiddenSegments>
               </requestFiltering>
          </security>
          <httpErrors existingResponse="PassThrough" />
          <iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
     </system.webServer>
</configuration>

【讨论】:

  • 我已经在 web.config 中进行了这些更改。但它没有奏效。
猜你喜欢
  • 2020-01-29
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多