Overview
On Windows Azure Pack, node.js is handled by the iisnode module. To run a node.js script, a handler mapping first has to be added to the site's web.config to process the .js script, or all .js scripts, using the iisnode module.
The node.js version installed by default is v0.10.5. That is old enough that most modern packages will not run on it, so in practice you will usually want to supply your own build. See the Custom node.js Version section below.
Handler Mapping
To have all .js scripts handled by the iisnode module, add module "iisnode" to the handlers element of the web.config as in the syntax example below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="iisnode" path="*.js" verb="*" modules="iisnode" />
</handlers>
</system.webServer>
</configuration>If there are other files using the .js extension in the application, necessitating targeting specific files, update the handler path. For example, if the below ztest.js file were in the document root, the syntax would be:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="ztest" path="ztest.js" verb="*" modules="iisnode" />
</handlers>
</system.webServer>
</configuration>Note that when adding a handler for more than one file, each will require a unique name.
Testing node.js
To test node.js, the syntax below may be copied, making sure to use the file extension .js. Or download and extract ztest.js, then upload the file to the site and visit the page in a browser.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello world');
}).listen(process.env.PORT);Verify it worked
Browse to the .js file. The page should return Hello world as plain text. If instead the browser downloads the file or shows its source, the handler mapping is not being applied: check that the web.config is in the same directory and that the handler path matches the file name.
Custom node.js Version
Visit https://nodejs.org/en/about/previous-releases and click the version number link under "Node.js" for the package download.
Then under "Binary Downloads" select the win x64 zip.
After downloading the zip, extract node.exe.
After extracting the file, upload it to the hosted site. When uploading the file, make sure to place node.exe outside the document root, or into a directory that only the application has access to, like /site/wwwroot/app_data. For this example, node.exe is being placed into the root directory of the site.
iisnode.yml
Create a new text file and name it "iisnode.yml". In that file add the following line:
nodeProcessCommandLine: "server path to file"
For the example above where node.exe was placed in the site root, the syntax would be:
nodeProcessCommandLine: "C:\home\node.exe"
To test the update, create a separate .js file and add the following syntax to display the version of node.js the site is using:
var http = require('http');
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Node.js ' + process.version);
}).listen(process.env.PORT);Or download node.zip for a sample iisnode.yml and test script. Then upload iisnode.yml (and the test script) to the document root.
Then viewing the test file through a browser should show the new node.js version.
Related Articles