Nodejs Express Router REST APIs using azure app service



我的 azure 帐户中有一个应用服务,该服务正在运行节点应用程序(Express、Angular 和 websockets(。我能够向用户提供静态内容,但 REST API 失败并出现 404 未找到错误。

以下是我的索引中的代码.js

var os = require('os');
var fs = require("fs");
var bodyParser = require('body-parser');
var express = require('express'),
    expressApp = express(),
    socketio = require('socket.io'),
    http = require('http'),
    uuid = require('node-uuid'),
    config = require('../config/config.json');
var httpServer = http.createServer(expressApp),
    rooms = {},
    userIds = {};
expressApp.use(express.static(__dirname + '/../public/dist/'));
expressApp.use(bodyParser.urlencoded({ extended: true }));
expressApp.use(bodyParser.json());
var router = express.Router();
expressApp.use('/api', router);
router.get('/getsomethings', function(req, res) {
    res.json(something);
});
expressApp.use('/api', router);
expressApp.listen = function listen() {
    httpServer.listen(config.PORT);
};
expressApp.listen();
exports.run = function(config) {
    //some code
};

以下是我的 web.config for app.service

<configuration>
  <system.webServer>
    <handlers>
      <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
      <add name="iisnode" path="index.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>        
        <!-- Don't interfere with requests for node-inspector debugging -->
        <clear />
        <rule name="Redirect to https" stopProcessing="true">
            <match url=".*" />
            <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
        </rule>
        <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 application entry point -->
        <rule name="DynamicContent">
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
          </conditions>
          <action type="Rewrite" url="index.js" />
        </rule>
      </rules>
    </rewrite>
    <security>
       <requestFiltering>
         <hiddenSegments>
           <add segment="node_modules" />
         </hiddenSegments>
       </requestFiltering>
     </security>
    <!-- You can control how Node is hosted within IIS using the following options -->
    <!--<iisnode      
          node_env="%node_env%"
          nodeProcessCountPerApplication="1"
          maxConcurrentRequestsPerProcess="1024"
          maxNamedPipeConnectionRetry="3"
          namedPipeConnectionRetryDelay="2000"      
          maxNamedPipeConnectionPoolSize="512"
          maxNamedPipePooledConnectionAge="30000"
          asyncCompletionThreadCount="0"
          initialRequestBufferSize="4096"
          maxRequestBufferSize="65536"
          watchedFiles="*.js"
          uncFileChangesPollingInterval="5000"      
          gracefulShutdownTimeout="60000"
          loggingEnabled="true"
          logDirectoryNameSuffix="logs"
          debuggingEnabled="true"
          debuggerPortRange="5058-6058"
          debuggerPathSegment="debug"
          maxLogFileSizeInKB="128"
          appendToExistingLog="false"
          logFileFlushInterval="5000"
          devErrorsEnabled="true"
          flushResponse="false"      
          enableXFF="false"
          promoteServerVars=""
         />-->
  </system.webServer>
</configuration>

当我访问我的网站时,除了/getsomethings XHR 给出 404 错误之外,一切正常。所有静态内容都很好。

谁能帮我找出问题所在?

你说">我能够为我的用户提供静态内容",因为你在web.config有这个。

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

根据您上面提供的代码,请确保您的文件夹结构如下所示:

D:homesite
├── config  
│   └── config.json
├── deployments 
│   └── ...
├── locks
│   └── ...
├── Diagnostics
│   └── ...
├── public
│   └── dist
│       └── ...
└── wwwroot
    ├── node_modules 
    │   └── ...
    ├── index.js
    ├── package.json
    └── web.config

另外,请尝试更改以下代码行:

httpServer.listen(config.PORT);

自:

httpServer.listen(process.env.PORT || config.PORT);

最新更新