Node.js Azure Web应用程序无法访问我的路线

时间:2018-09-19 09:19:51

标签: javascript node.js azure azure-web-app-service

我有以下index.js文件:

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

以及以下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="^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>

现在我将其添加到服务器中,使其看起来像这样:

enter image description here

然后我尝试永久使用forever start index.js启动服务器 然后我去我的网站并尝试一条路线,但这只给我一个错误代码500。

有人可以告诉我我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

根据我的经验,您可以像下面那样修改scriptParsed并重试:

index.js

同样,对于500错误,您将需要启用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')); }); stdout的日志记录以进行故障排除,并查看日志内容。要启用调试,请按照以下步骤操作:

1)如果不存在,请在根文件夹(stderr)中创建文件iisnode.yml

2)在其中添加以下几行。

D:\home\site\wwwroot

完成后,您可以在loggingEnabled: true logDirectory: iisnode 中找到日志。

有关更多信息,请参阅doc

希望它对您有帮助。