Run Node. js IIS extension iisnode installation configuration notes, node. jsiisnode

Source: Internet
Author: User

Run Node. js IIS extension iisnode installation configuration notes, node. jsiisnode

At the beginning of this year, I plan to use Node. js to rewrite the blog program based on the Express framework, and then I will 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:
Copy codeThe Code is as follows:
<Configuration>
<System. webServer>
<Handlers>
<Add name = "iisnode" path = "bin/www" verb = "*" modules = "iisnode" resourceType = "Unspecified" requireAccess = "Script"/>
</Handlers>

<Rewrite>
<Rules>
<Rule name = "all">
<Match url = "/*"/>
<Action type = "Rewrite" url = "bin/www"/>
</Rule>
</Rules>
</Rewrite>
</System. webServer>
</Configuration>

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:
Copy codeThe 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:
Copy codeThe Code is as follows:
<Configuration>
<System. webServer>
<Handlers>
<Add name = "iisnode" path = "launch/www" verb = "*" modules = "iisnode" resourceType = "Unspecified" requireAccess = "Script"/>
</Handlers>

<Rewrite>
<Rules>
<Rule name = "all">
<Match url = "/*"/>
<Action type = "Rewrite" url = "launch/www"/>
</Rule>
</Rules>
</Rewrite>
</System. webServer>
</Configuration>

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.

In Web. config, You can retain REMOTE_ADDR before adding a configuration (before </system. webServer>:
Copy codeThe Code is as follows:
<Iisnode promoteServerVars = "REMOTE_ADDR"/>

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:
Copy codeThe 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:
Copy codeThe 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:
Copy codeThe Code is as follows:
#! /Usr/bin/env node

Require ('./bin/www ');

Then change the program entry to launch. js:

Copy codeThe Code is as follows:
<Configuration>
<System. webServer>
<Handlers>
<Add name = "iisnode" path = "launch. js" verb = "*" modules = "iisnode" resourceType = "Unspecified" requireAccess = "Script"/>
</Handlers>

<Rewrite>
<Rules>
<Rule name = "all">
<Match url = "/*"/>
<Action type = "Rewrite" url = "launch. js"/>
</Rule>
</Rules>
</Rewrite>

<Iisnode promoteServerVars = "REMOTE_ADDR"/>
</System. webServer>
</Configuration>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.