使用azure app服务的Nodejs Express Router REST API

时间:2017-06-15 13:56:40

标签: node.js azure iisnode

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

以下是我的index.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
};

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

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

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

任何人都可以帮我弄清楚出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

您说“我可以向我的用户提供静态内容”,因为您在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:\home\site
├── 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);