Run node.js IIS Extensions iisnode install configuration notes _node.js

Source: Internet
Author: User
Tags file upload

Early this year to use Node.js based on the Express Framework rewrite blog program, Farewell asp.net. However, my current VPS is a Windows Server system, an IIS server, and if both Express and IIS are listening to port 80, there is a clear conflict. Fortunately, there is an extension called Iisnode that can host Node.js programs to IIS. Also, this hosting means that you can use the various functions within IIS (process management, gzip compression, logging, caching, permissions control, domain binding, and so on).

To use Iisnode, you have to install:

1.node.js
2.IIS URL Rewrite module
3.iisnode

After installing, or follow the normal operation, create a site in IIS Manager, point to the Express program directory, the key is to add a Web.config file:

Copy Code code as follows:

<configuration>
<system.webServer>
<add name= "Iisnode" path= "bin/www" verb= "*" modules= "Iisnode" resourcetype= "Unspecified" requireaccess= "Script"/ >

<rewrite>
<rules>
<rule name= "All" >
<match url= "/*"/>
<action type= "Rewrite" url= "Bin/www"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

This content can also be configured through the visual interface of IIS Manager. The idea is to rewrite all requests to bin/www and run bin/www with Iisnode extensions. However, when you open the site, you receive this error message:

Copy Code code as follows:

The request filtering module is configured to reject the path in the URL containing the hiddensegment section

At first it felt unclear so, then suddenly wake up, ASP. NET Bin directory is a special directory that is not allowed to access. Rewrite the request to bin/www and hit the rule exactly. So, change the directory name, such as Change bin to launch (it turns out that this is not good practice, later), Web.config also to adjust:
Copy Code code as follows:

<configuration>
<system.webServer>
<add name= "Iisnode" path= "launch/www" "verb=" * "modules=" Iisnode "resourcetype=" Unspecified "requireaccess=" Script "/>

<rewrite>
<rules>
<rule name= "All" >
<match url= "/*"/>
<action type= "Rewrite" url= "Launch/www"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Restart the site in IIS Manager after the visit again, finally run up, not easy AH! But it was too early to be happy.

In the process of testing the function of the program, unexpectedly found that the IP is empty. In the Express framework, IP is acquired through REQ.IP, and Req.ip is obtained from the remote_addr of the request header. A simple test code found that the value of REMOTE_ADDR was also empty. Obviously, this header information was lost during the process from IIS to Node.js. After Google, it was found that Iisnode did, and the official solution was to use x-forword-for, but I found another way.

A section of the Web.config (added to </system.webServer>) can retain REMOTE_ADDR:

Copy Code code as follows:

<iisnode promoteservervars= "REMOTE_ADDR"/>

According to the instructions, the reserved remote_addr will be renamed X-IISNODE-REMOTE_ADDR, so the REQ.IP value should be overwritten once, adding a middleware function to the Express app.js:

Copy Code code as follows:

App.use (function (req, res, next) {
Req.ip = req.headers[' x-iisnode-remote_addr '];
Next ();
});

However, this adjustment, access to the IP or empty, which inevitably makes people doubt that Req.ip's assignment is not a failure. Look at the source code of Express can be found, REQ.IP is defined by the way of define getter, so to cover it will have to define once again:
Copy Code code as follows:

App.use (function (req, res, next) {
Object.defineproperty (req, ' IP ', {
Get:function () {return this.headers[' x-iisnode-remote_addr '];}
});
Next ();
});

This problem has finally been solved, but this is not a good way, if the express Req.ip set into a read-only trouble.

Continue testing and find another problem. Normally, the file upload function of the blog backstage will upload the file to Public/upload, 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 as a program entry is in the launch directory, so the launch directory becomes the execution directory for the application. My solution is to change the name of the launch directory back to bin and create a launch.js in the root directory to invoke Bin/www:

Copy Code code as follows:

#!/usr/bin/env node

Require ('./bin/www ');

Then change the procedure entry to Launch.js:

Copy Code code as follows:

<configuration>
<system.webServer>
<add name= "Iisnode" path= "launch.js" verb= "*" modules= "Iisnode" resourcetype= "Unspecified" requireaccess= "Script" />

        <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 a mature product, of course node.js is not (so far not 1.0), all of them need further exploration and improvement.

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.