【问题标题】:iisnode won't run Expressiisnode 不会运行 Express
【发布时间】:2016-02-22 20:41:58
【问题描述】:

我正在尝试使用 iisnode 运行 express。我按照提供的示例进行操作,但是在尝试将最新的 Express 版本与基本示例一起使用时,无法使其正常工作。

我收到错误 Cannot GET /node/parislight/hello.js 和其他时候,只是 The webpage cannot be found

我创建了一个取自the express docshello.js 文件(主要快递文件)。

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

var server = app.listen(process.env.PORT, function () {

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

  console.log('Example app listening at http://%s:%s', host, port)

})

我添加了必要的web.config 文件(从 iisnode 中的 express 示例中提取)

<configuration>
  <system.webServer>

    <!-- indicates that the hello.js file is a node.js application 
    to be handled by the iisnode module -->

    <handlers>
      <add name="iisnode" path="hello.js" verb="*" modules="iisnode" />
    </handlers>

    <!-- use URL rewriting to redirect the entire branch of the URL namespace
    to hello.js node.js application; for example, the following URLs will 
    all be handled by hello.js:

        http://localhost/node/express/myapp/foo
        http://localhost/node/express/myapp/bar

    -->

    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="myapp/*" />
          <action type="Rewrite" url="hello.js" />
        </rule>
      </rules>
    </rewrite>

  </system.webServer>
</configuration>

我为 IIS 中使用的应用程序池授予了所有必要的权限。

【问题讨论】:

    标签: node.js iis express iis-8 iisnode


    【解决方案1】:

    需要使用完整路径:

    app.get 调用中指定的路径必须是请求的完整路径。

    Source

    现在看起来像这样:

    app.get('/node/parislight/myapp/demo', function (req, res) {
      res.send('Hello World!')
    })
    

    通过以下方式加入:

    http://localhost/node/parislight/myapp/demo

    【讨论】:

      【解决方案2】:

      您也可以在文件末尾使用 catch all 函数来获取任何 url 并发送 200 响应或发送您的特殊 404 页面。注意:放置必须在所有其他指定的 url 之后。

      app.get('*', function (req, res) {
        res.send('Hello World!')
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-10
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多