发布快速服务器以进行天蓝色

时间:2019-02-11 14:44:36

标签: azure express

我是 nodejs 的新手。我有一个非常简单的express应用程序(仅包含一个javascript文件(index.js),看起来像这样:

  const express = require('express');
const bodyParser = require('body-parser');
const {
  对话流
} = require('googles行动');

const PORT = process.env.PORT || 4000;
const app = dialogflow({
  调试:true
});
const服务器= express()
  .use(bodyParser.urlencoded({
    扩展:true
  }))
  .use(bodyParser.json(),app)
  .listen(PORT,()=> console.log(`在$ {PORT}上监听)));
 

当我将其发布为天蓝色时,我将使用以下设置生成一个web.config文件:

  -Handler iisnode -NodeStartFile index.js -appType节点
 

将生成如下所示的web.config:

  <?xml version =“ 1.0” encoding =“ utf-8”?>
<!-
    如果使用iisnode在后面运行节点进程,则需要此配置文件
    IIS或IIS Express。有关更多信息,请访问:

    https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
->

<配置>
  
    <!-访问http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx,以获取有关WebSocket支持的更多信息->
    
    <处理程序>
      <!-表示server.js文件是要由iisnode模块处理的node.js站点->
      
    
    <重写>
      <规则>
        <!-不要干扰节点检查器调试的请求->
        
          
        

        <!-首先,我们考虑传入的URL是否与/ public文件夹中的物理文件匹配->
        
          
        

        <!-所有其他URL都映射到node.js网站入口点->
        
          <条件>
            
          
          
        
      
    

    <!-'bin'目录在node.js中没有特殊含义,可以在其中放置应用程序->
    <安全性>
      
        
          
        
      
    

    <!-确保错误响应保持不变->
    

    <!-
      您可以使用以下选项控制如何在IIS中托管Node:
        * watchedFiles:用分号分隔的文件列表,将在监视文件更改时重新启动服务器
        * node_env:将作为NODE_ENV环境变量传播到节点
        * debuggingEnabled-控制是否启用内置调试器

      有关选项的完整列表,请参见https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
    ->
    <!-->
  

 

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

1 个答案:

答案 0 :(得分:1)

似乎您从Using a custom web.config for Node apps复制了web.config文件的内容,并在代码server.js中将第一个index.js的值替换为<add name="iisnode" path="index.js" verb="*" modules="iisnode"/>

但是,还需要在标签server.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>

将所有server.js替换为index.js之后,重新启动Web应用程序并在浏览器中刷新页面,然后您可以看到它正常工作。

注意:Azure WebApp的默认Node.js版本为0.10.40,因此首先您需要通过在应用程序设置中设置WEBSITE_NODE_DEFAULT_VERSION值来更改其版本,请参见我对SO线程的回答Azure NodeJS version知道该怎么做。