This article mainly introduces how to run Node. js IIS extension iisnode installation configuration notes, iisnode extension can put Node. js programs are hosted in IIS. after hosting, various functions in IIS can also be used. if you need them, refer to your plans to use Node at the beginning of this year. js rewrite the blog program based on the Express framework, and then say goodbye to ASP.. NET. However, the current vps I use is a Windows Server system or an IIS Server. if both Express and IIS listen to port 80, there will be a major conflict. Fortunately, an extension called iisnode can host Node. js programs to IIS. In addition, after hosting, you can also use various features in IIS (process management, GZip compression, logs, cache, permission control, domain name binding, etc ).
To use iisnode, install:
1. Node. js
2. iis url Rewrite Module
3. iisnode
After installation, follow the regular operations to create a site in the IIS Manager and point to the Directory of the Express program. The key is to add a web. config file:
The code is as follows:
This section can also be configured through the visual interface of the IIS Manager. It means to rewrite all requests to bin/www, and run bin/www using iisnode extension. However, after the site is opened, the following error occurs:
The code is as follows:
The request filtering module is configured to reject the path in the URL containing the hiddenSegment section.
The bin directory in ASP. NET is a special directory that cannot be accessed. Rewrite the request to bin/www, which hits this rule. So, just change the directory name. for example, change bin to launch (it turns out that this is not a good practice, and we will talk about it later). the web. config should also be adjusted accordingly:
The code is as follows:
It's not easy to restart the site in IIS Manager and access the site again! However, I am happy too early.
In the process of testing program functions, we found that the obtained IP address was empty. In the Express framework, IP is obtained through req. ip, and req. ip is obtained from REMOTE_ADDR in the request header. A simple test code shows that the value of REMOTE_ADDR is empty. Obviously, the header information is lost during the process from IIS to Node. js. After Google's review, I found that iisnode had this problem. The official solution was to use X-Forword-For, but I found another solution.
There is a configuration in Web. config (added) You can retain REMOTE_ADDR:
The code is as follows:
According to the instructions, the reserved REMOTE_ADDR will be renamed to the x-iisnode-REMOTE_ADDR, so you have to overwrite the req. ip value once, add a middleware function in Express app. js:
The code is as follows:
App. use (function (req, res, next ){
Req. ip = req. headers ['X-iisnode-REMOTE_ADDR '];
Next ();
});
However, after this adjustment, the obtained IP address is still blank, which may cause suspicion that the assignment of req. ip fails. Looking at the source code of Express, we can find that req. ip is defined by define getter, so to overwrite it, we have to define it again:
The code is as follows:
App. use (function (req, res, next ){
Object. defineProperty (req, 'IP ',{
Get: function () {return this. headers ['X-iisnode-REMOTE_ADDR '];}
});
Next ();
});
This problem is finally solved, but this is not a good method. it would be too difficult if Express sets req. ip to read-only in the future.
Continue the test and find another problem. Normally, the file upload function in the blog background uploads files to the public/upload directory, but in fact the public/upload folder is generated under the launch Directory (that is, the original bin directory. The reason is that the www file used as the program entry is under the launch directory, so the launch directory becomes the execution Directory of the application. My solution is to change the launch directory name back to bin and create a launch. js under the root directory to call bin/www:
The code is as follows:
#! /Usr/bin/env node
Require ('./bin/www ');
Then change the program entry to launch. js:
The code is as follows:
Obviously, iisnode is not yet a mature product, and of course Node. js is not (no more than 1.0 so far). everything needs to be further explored and improved.