node.js - 禁止访问 Azure 中的 Node.js 源代码文件

标签 node.js azure

我正在使用 Node.js 构建一个基本网站,并且我不希望任何人能够访问我的服务器端源代码(它包含数据库的登录凭据)。我的主应用程序位于运行应用程序的根目录中名为“app.js”的文件中。如果我浏览到 mysite.com/app.js,则会提供源代码文件。有没有办法禁止使用 Node.js 或一般情况下访问某些文件?该网站托管在 Microsoft Azure 上,如果这有什么不同的话(我的研究似乎表明 Microsoft 和 Apache 对此的处理方式不同)。

最佳答案

基本上,Azure 使用 IIS 来为您的 Node.js 应用程序提供服务。因此,您需要将名为 web.config 的 IIS 配置文件添加到应用的根文件夹中,以限制对服务器端源代码的访问。

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 app.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

               To debug your node.js application:
                 * set the debuggingEnabled option to "true"
                 * enable web sockets from the portal at https://manage.windowsazure.com/#Workspaces/WebsiteExtension/Website/aarontestnode/configure
                 * browse to https://aarontestnode.azurewebsites.net/app.js/debug/

               See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
          -->
          <iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
     </system.webServer>
</configuration>

关于node.js - 禁止访问 Azure 中的 Node.js 源代码文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44256847/

相关文章:

javascript - 多个提交按钮 - html express node js

javascript - 存储组件环回上传文件

javascript - 从嵌套对象和数组的巨大 JSON 中以编程方式解构特定属性的最佳且最有效的方法

java - 如何通过postman调用REST API在Azure中创建索引?

c# - 部署到 Windows Azure 模拟器时 Windows 8 计算机重新启动

c# - Azure AD 为 "external provider"?

node.js - 通过 Node 流式传输连续的 HTTP 响应

javascript - Hapi JS 路由处理程序中的 JS 变量范围

c# - WP7.1应用程序中的WebClient仅调用一次

python - 使用 conda 环境在 VS code 中部署 Azure Functions