简体   繁体   中英

Azure App Service adding '/' in URL

I'm working on a node.js application deployed on Azure App Service.

The application runs as expected on localhost-

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

But when deployed on Azure, the URL acts weird-

http://<appname>.azurewebsites.net/search/?q=pink+floyd Extra '/' before query string.

My Express.js code is-

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

HTML page serving query-

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

I can't reproduce your issue on Azure App Service with Express. However, the following is what I tried so far.

My folder structure:

在此处输入图片说明

My app.js code looks like:

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!')
})

My test.html code:

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

Tested result:

在此处输入图片说明

And could you share the web.config file which you are using? My guess is you're missing out on a simple thing there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM