Azure App Service在URL中添加“/”

时间:2017-02-02 04:29:56

标签: javascript node.js azure express

我正在开发部署在Azure App Service上的node.js应用程序。

应用程序在localhost上按预期运行 -

http://localhost:55231/search?q=pink+floyd

但是当部署在Azure上时,URL行为很奇怪 -

在查询字符串之前

http://<appname>.azurewebsites.net/search/?q=pink+floyd额外'/'。

我的Express.js代码是 -

app.get('/search', function (req, res) {
  response = {
  search_text:req.query.q,
  };
//Few more code

HTML页面提供查询 -

<form action = "/search" method = "GET">
  <input type = "text" name = "q" placeholder=" Search your Favourite Music">
  <input type = "submit" value = "Search">
</form>

1 个答案:

答案 0 :(得分:1)

我无法使用Express在Azure App Service上重现您的问题。但是,以下是我到目前为止所尝试的内容。

我的文件夹结构:

enter image description here

我的 app.js 代码如下:

var express = require('express')
var app = express()

app.use(express.static('public'))

app.get('/', function (req, res) {
  res.send('Hello Azure!')
})

app.get('/search', function(req, res) {
  res.send(200, 'Test')
})

app.listen(process.env.PORT || 3000, function () {
  console.log('Example app listening on port 3000!')
})

我的 test.html 代码:

<html>
  <head>
    <title>Test</title>
  </head>

  <body>
    <form action="search" method="GET">
      <input type="text" name="q" />
      <button type="submit">Search</button>
    </form>
  </body>
</html>

的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/aaronexpress/configure
                 * browse to https://aaronexpress.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>

测试结果:

enter image description here

你能分享你正在使用的 web.config 文件吗?我的猜测是你错过了一个简单的事情。