iisnode won't run Express

2019-07-07 17:50发布

I'm trying to run express using iisnode. I followed the examples provided but when trying to use the latest Express version with a basic example, there's no way to make it work.

I'm getting the error Cannot GET /node/parislight/hello.js and other times, just a The webpage cannot be found.

My created a hello.js file (main express file) taken from the express docs.

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)

})

I added the necessary web.config file (extracted from the express example within iisnode)

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

I gave all the necessary permissions to the used App Pool in IIS.

2条回答
我只想做你的唯一
2楼-- · 2019-07-07 18:13

It needs to use the full path:

The path specified in app.get calls must be the full path of the request.

Source

Looking like this now:

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

Acceding to it through:

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

查看更多
乱世女痞
3楼-- · 2019-07-07 18:17

You can use a catch all function at the end of your file as well to get any url and send a 200 response or send your special 404 page. Note: placement has to be after all other specified urls.

app.get('*', function (req, res) {
  res.send('Hello World!')
})
查看更多
登录 后发表回答