Node.js on Windows

From Hostek.com Wiki
Revision as of 21:24, 28 December 2017 by Codyw (Talk | contribs) (updated grammar, examples, troubleshooting, and outline)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Installing Node.js on a Windows VPS

Node.js

Note that if you have Node.js already installed or the executable already on your server, then you can skip this step.

  1. Download and install the Node.js installer (MSI) for Windows: https://nodejs.org/download/
    • Be sure to install the correct version that matches your server's bitness.
  2. Accept the defaults and finish installing Node.js.

You can confirm that you've successfully installed Node.js by following these steps.

  1. Launch a new command prompt(cmd).
  2. Run 'node --version' and it should print out the Node.js version installed on your server.

iisnode

If you want to host Node.js applications with IIS, then you'll need to use the "iisnode" IIS module.

  1. Download and install the latest stable iisnode module from: https://github.com/Azure/iisnode/releases
    • Be sure to install the correct version that matches your server's bitness.
  2. Add "list folder" permissions on c:\home for IIS_IUSRS. You can use the "Advanced" permissions control to apply the permission to "This folder only". Caution: If you're hosting multiple applications for various customers you might need to make further security considerations.
    • This assumes that you're using the default site path of 'C:\Home\domain.tld' that we configure on Windows VPS servers. If you have a custom setup, then you'll need to be sure that the 'IIS_IUSRS' group has "list folder" permissions for all directories leading up to your site's webroot.

Running Your First Node.js Application

Configure your site for iisnode

For your Node.js application to work with IIS, you will need to configure the site for use with iisnode. You can accomplish this with some web.config options, or a web.config option as well as a .yml config file. For the sake of simplicity and getting started quickly, we've provided a couple of examples below.

The only changes you'll probably need to make in the example yaml config are the paths to the node.exe executable and the interceptor.

web.config:

<configuration>
<system.webServer>
 
    <handlers>
      <add name="iisnode" path="node_app.js" verb="*" modules="iisnode" />
    </handlers>
 
    <rewrite>
      <rules>
        <rule name="nodejs">
          <match url="(.*)" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          </conditions>
          <action type="Rewrite" url="/node_app.js" />
        </rule>
      </rules>
    </rewrite>
 
    <security>
      <requestFiltering>
        <hiddenSegments>
          <add segment="node_modules" />
          <add segment="iisnode" />
        </hiddenSegments>
      </requestFiltering>
    </security>
 
  </system.webServer>
</configuration>

iisnode.yml:

# The optional iisnode.yml file provides overrides of   
# the iisnode configuration settings specified in web.config.  
  
node_env: production  
nodeProcessCommandLine: "C:\Program Files\nodejs\node.exe"  
interceptor: "C:\Program Files\iisnode\interceptor.js"  
nodeProcessCountPerApplication: 1  
maxConcurrentRequestsPerProcess: 1024  
maxNamedPipeConnectionRetry: 24  
namedPipeConnectionRetryDelay: 250  
maxNamedPipeConnectionPoolSize: 512  
maxNamedPipePooledConnectionAge: 30000  
asyncCompletionThreadCount: 0  
initialRequestBufferSize: 4096  
maxRequestBufferSize: 65536  
watchedFiles: *.js;iisnode.yml  
uncFileChangesPollingInterval: 5000  
gracefulShutdownTimeout: 60000  
loggingEnabled: true  
logDirectory: iisnode  
debuggingEnabled: true  
debuggerPortRange: 5058-6058  
debuggerPathSegment: debug  
maxLogFileSizeInKB: 128  
maxTotalLogFileSizeInKB: 1024  
maxLogFiles: 20  
devErrorsEnabled: false  
flushResponse: false  
enableXFF: false  
promoteServerVars:

Create your application

For the most part, your Node.js application should work as expected. It's important to note that there are some differences. For example, the express .listen() method works differently with iisnode apps, which you can see later in the wiki. If necessary, you can find a handful of example applications in the iisnode github repo: https://github.com/Azure/iisnode/tree/master/src/samples/

In our example, we've created a very simple express application. You'll need to use npm to install the "express" node module. To do this, navigate to your site's web root in command prompt or powershell and then run the following command:

npm install express --save

Here is our example application:

node_app.js:

var express = require('express');
var app = express();
app.get('/', function (req, res) {
	res.send('Hello World!');
});
// This is REQUIRED for IISNODE to work
app.listen(process.env.PORT, () => {
	console.log('listening')
})

If you browse to your site, you should see the text "hello world".

If you need a simpler example that requires no 3rd party modules, then you can use this one from the iisnode github repo: https://github.com/Azure/iisnode/blob/master/src/samples/helloworld/hello.js

Troubleshooting Your Node.js Application

If you followed this wiki, then logging should be enabled for your application. The standard output and standard error messages should be logged to their own respective log files in the "iisnode" directory.

Common Errors

Error: EPERM: operation not permitted, lstat 'C:\home'

This means that your site's web user doesn't have permission to list the folder contents. Double-check the permissions of your site's web user and make sure that it can list the contents of all directories leading up to your site's web root.