IIS 节点应用程序不会 运行 与 Web.Config 正确?

IIS Node Application won't run correctly with Web.Config?

我有一个节点应用程序,我想 运行 使用 IIS。问题是我的配置文件。我正在使用 IIS 10.0,不知道如何正确配置所有内容?每当我在 Web.Config 文件中包含这两个规则时...

<!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
<rule name="StaticContent" patternSyntax="Wildcard">
    <action type="Rewrite" url="public/{R:0}" logRewrittenUrl="true"/>
        <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        </conditions>
        <match url="*.*"/>
</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="server.js"/>
 </rule>

...我收到如下所示的 500.1000 错误:

500.1000 ERROR

当我删除一个或另一个时,我的网站页面和链接正确加载,但所有 POST 请求都会产生 404 错误?因为它正在搜索物理路径而不是在我的 server.js 文件中调用 POST 函数?

这是 404 错误:

404 ERROR

我知道我的网站可以运行,因为它在 VS 2015 的调试器中运行(GET 和 POST 请求)。以下是我的代码和配置文件的片段。

这是完整的 Web.Config 文件:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
    <appSettings>
        <!--
            All appSettings are made available to your Node.js app via environment variables
            You can access them in your app through the process.env object.

            process.env.<key>
        -->

       <!-- Unconmment the below appSetting if you'd like to use a Virtual Directory -->
       <!-- <add key="virtualDirPath" value="" /> -->
    </appSettings>
    <system.webServer>
        <iisnode node_env="%node_env%"
                 nodeProcessCountPerApplication="1"
                 maxConcurrentRequestsPerProcess="1024"
                 maxNamedPipeConnectionRetry="100"
                 namedPipeConnectionRetryDelay="250"
                 maxNamedPipeConnectionPoolSize="512"
                 maxNamedPipePooledConnectionAge="30000"
                 asyncCompletionThreadCount="0"
                 initialRequestBufferSize="4096"
                 maxRequestBufferSize="65536"
                 uncFileChangesPollingInterval="5000"
                 gracefulShutdownTimeout="60000"
                 loggingEnabled="true"
                 logDirectory="iisnode"
                 debuggingEnabled="true"
                 debugHeaderEnabled="false"
                 debuggerPortRange="5058-6058"
                 debuggerPathSegment="debug"
                 maxLogFileSizeInKB="128"
                 maxTotalLogFileSizeInKB="1024"
                 maxLogFiles="20"
                 devErrorsEnabled="true"
                 flushResponse="false"
                 enableXFF="false"
                 promoteServerVars=""
                 configOverrides="iisnode.yml"
                 watchedFiles="web.config;*.js"
                 nodeProcessCommandLine="C:\Program Files\nodejs\node.exe"/>

        <!-- 
            Before the handlers element can work on IIS 8.5
            follow steps listed here https://github.com/tjanczuk/iisnode/issues/52 
        -->                 
        <handlers>
            <add name="iisnode" path="server.js" verb="*" modules="iisnode"/>

            <!-- Uncomment below handler if using Socket.io -->
            <!--<add name="iisnode-socketio" path="server.js" verb="*" modules="iisnode" />-->
        </handlers>

        <rewrite>
            <rules>
                <!-- Don't interfere with requests for node-inspector         debugging -->
                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^server.js\/debug[\/]?"/>
                </rule>

                <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                <rule name="StaticContent" patternSyntax="Wildcard">
                    <action type="Rewrite" url="public/{R:0}" logRewrittenUrl="true"/>
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                    </conditions>
                    <match url="*.*"/>
                </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="server.js"/>
                </rule>

                <rule name="SocketIO" patternSyntax="ECMAScript">
                    <match url="socket.io.+"/>
                    <action type="Rewrite" url="server.js"/>
                </rule>
            </rules>
        </rewrite>
        <directoryBrowse enabled="false"/>
    </system.webServer>
</configuration>

这是我的 server.js 文件的一般结构:

var http = require('http');
var express = require('express');
var fs = require("fs");
var sql = require("mssql");
var bodyParser = require('body-parser');         // Used to parse incoming messages
var Connection = require('tedious').Connection;  // Added for Azure

var app = express();
var port = process.env.port || 1337;

// pulls the index.html file
app.use(express.static(__dirname + "/"));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

// DATABASE configuration
var config = {
    userName: 'USERNAME',
    password: 'PASSWORD',
    server:   'SERVER',   
    options: {
        encrypt: true,
        database: 'DATABASE',
        rowCollectionOnRequestCompletion: true
    }
};
var connection = new Connection(config);


// Server that listens to port 80
var server = http.createServer();

var server = app.listen(port, function () {

    var host = server.address().address
    var port = server.address().port

    console.log("Node Js app listening at http://%s:%s", host, port)

});



app.post('/postrequest', function (req, res) {
    // function 
});

感谢您的帮助。我似乎无法弄清楚这个问题。

我在单独的脚本中发出 post 请求,该脚本位于此目录中名为脚本的单独文件夹中:

xhttp.open("POST", "http://localhost:80/My Application/postrequest", false);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("date=" + date + "&name=" + name + "&type=" + type);

通过在我的 Web.Config 中使用以下配置,我能够成功地将我的 Node Web 应用程序发布到 IIS:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>

    <rewrite>
      <rules>
         <!-- Don't interfere with requests for node-inspector debugging -->
         <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="^server.js\/debug[\/]?"/>
         </rule>

         <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
         <rule name="StaticContent" patternSyntax="Wildcard">
               <action type="Rewrite" url="public/{R:0}" logRewrittenUrl="true"/>
               <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
               </conditions>
            <match url="*.*"/>
         </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="server.js"/>
         </rule>

         <rule name="SocketIO" patternSyntax="ECMAScript">
               <match url="socket.io.+"/>
            <action type="Rewrite" url="server.js"/>
         </rule>
      </rules>
    </rewrite>

    <iisnode promoteServerVars="LOGON_USER" />

  </system.webServer>
</configuration>

并将我的 server.js 文件更改为:

var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');         // Used to parse incoming messages
var port = process.env.port;

var app = express(function(req, res){
    username = req.headers['x-iisnode-auth_user'];
});

app.use(express.static(__dirname + "/ApplicationNameOnIIS/"));

app.use(bodyParser.urlencoded({ extended: false }))

app.use(bodyParser.json())

// Start listening on server port
app.listen(process.env.PORT);

app.get('/ApplicationNameOnIIS/', function(req, res) {
    res.sendfile(__dirname + '/index.html');    
});

// ...The Rest Of My Code...