简体   繁体   中英

Node.js azure web app can't access my routes

I have the following index.js file:

const express = require('express');
const app = express();
const router = express.Router();
var cors = require('cors');
var http = require('http').Server(app);
const fileUpload = require('express-fileupload');
app.use(fileUpload());
const uploadRoute = require('./routes/upload');

app.use(cors());

app.use(uploadRoute(router));
app.use(router);
http.listen(80, function () {
    console.log('listening on *:80');
});

And the following web.config file:

    <?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="^index.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="index.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>

Now i add this to my server which makes it look like this:

在此处输入图片说明

I then attempt to start the server using forever forever start index.js then i go to my website and try out a route but it just gives me an error code 500.

Can anyone tell me what ive done wrong?

Per my experience, you could modify your index.js like below and try again:

const express = require('express');
const app = express();
const router = express.Router();
var cors = require('cors');
var http = require('http').Server(app);
const fileUpload = require('express-fileupload');
app.use(fileUpload());
const uploadRoute = require('./routes/upload');

app.use(cors());

app.use(uploadRoute(router));
app.use(router);

app.set('port', process.env.PORT || 5000);

app.listen(app.get('port'), function(){
          console.log('Express server listening on port ' + app.get('port'));
          });

Also, for 500 error, you'll need to enable logging of stdout and stderr for troubleshooting and see what the logs say. To enable debugging, please follow the steps:

  1. Create file iisnode.yml in your root folder ( D:\\home\\site\\wwwroot ) if not exists.

  2. Add the following lines to it.

    loggingEnabled: true logDirectory: iisnode

After that done, you can find logs in D:\\home\\site\\wwwroot\\iisnode .

For more info, please refer to the doc .

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