代码之家  ›  专栏  ›  技术社区  ›  r3plica

将Express服务器发布到Azure

  •  0
  • r3plica  · 技术社区  · 5 年前

    我是新来的 诺迪斯 . 我有一个非常简单的Express应用程序(只包含一个javascript文件(index.js),如下所示:

    const express = require('express');
    const bodyParser = require('body-parser');
    const {
      dialogflow
    } = require('actions-on-google');
    
    const PORT = process.env.PORT || 4000;
    const app = dialogflow({
      debug: true
    });
    const server = express()
      .use(bodyParser.urlencoded({
        extended: true
      }))
      .use(bodyParser.json(), app)
      .listen(PORT, () => console.log(`Listening on ${ PORT }`));
    

    当我将此发布到Azure时,我使用此设置生成一个web.config文件:

    -Handler iisnode -NodeStartFile index.js -appType node
    

    它生成这样的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="index.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="^server.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="server.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>
    

    但它似乎不起作用。有人知道我是否做错了什么吗?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Peter Pan    5 年前

    似乎你复制了 web.config 文件从 Using a custom web.config for Node apps 替换第一个 server.js 用价值 index.js 代码中 <add name="iisnode" path="index.js" verb="*" modules="iisnode"/> .

    但是,还有两个 服务器,JS 也需要在标记中替换的值 rewrite ,如下所示。

    <rewrite>
      <rules>
        <!-- Do not interfere with requests for node-inspector debugging -->
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js\/debug[\/]?" /> // Replace `server.js` at here
        </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="index.js"/> // Replace `server.js` at here
        </rule>
      </rules>
    </rewrite>
    

    全部更换后 服务器,JS 具有 索引文件 ,若要重新启动webapp并在浏览器中刷新页面,则可以看到它工作正常。

    注意:Azure webapp上的默认node.js版本是 0.10.40 ,因此首先需要通过设置 WEBSITE_NODE_DEFAULT_VERSION 应用程序设置中的值,请参阅我对so线程的回答 Azure NodeJS version 知道怎么做。