iisnode and express

2019-04-09 05:07发布

I'm sure this question has been asked a million times but I've not found any solution to the following problem as of yet.

I have a basic application running using nodejs and express. When I run the application in node, it by default sends me to the index.html in my public folder. Even if I don't have any routes set up. I want this to happen.

I've installed iisnode and created a new application under 'Default Website' called 'Devices'. I put my application in there unchanged. Reading around I needed to sort some stuff out with the web.config and decided to go with the config mentioned here (not the yaml):

http://tomasz.janczuk.org/2012/05/yaml-configuration-support-in-iisnode.html

When I try and load the app up in my browser it always tries to find the route in my app.js and throws messages back at me such as:

Cannot GET /devices/

I'm really pulling my hair out with this one and don't know what to do! Do I have to set up a default route for static content inside of my app.js entry point? I have the following in place:

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(path.join(__dirname, 'public')));
});

But it still doesn't hand me over to index.html when I load the application from it's root:

'http://localhost/devices/'

Anyone able to help me out with this?

2条回答
太酷不给撩
2楼-- · 2019-04-09 05:07

The easiest way to address this problem is to create a dedicated IIS website for your application as opposed to creating an IIS application within an IIS website like you did.

If you have a dedicated website for your node.js application in IIS, the application is accessible at the root of the URL space, e.g. http://myapp.com/, which is what the default express route configuration expects. In contrast, if you create an IIS application within an IIS website to host your node.js application (say devices), it is accessible at a URL subordinate to the root URL of the web site, e.g. http://myapp.com/devices/. The extra URL path segment (devices) is something your express app does not expect and is not configured to process, and therefore it rejects the request.

If you insist on hosting the node.js application as an IIS application in IIS, you will need to augment your express route configuration to expect the extra URL path segment that matches the IIS application name.

查看更多
霸刀☆藐视天下
3楼-- · 2019-04-09 05:28

A good suggestion can be found at: Can an iisnode-hosted web application work out the virtual path at which it is hosted? Essentially, you use one small variable to abstract the path and an environment variable (if it exists) to control it. Another good post (possibly better solution) can be found at https://github.com/tjanczuk/iisnode/issues/160

查看更多
登录 后发表回答