node.js on Windows IoT

Hello World application using node.js on a Windows IoT-capable Raspberry Pi 2

I’m a big fan of node.js on embedded devices and was pleased to hear that Microsoft would be making node available for Windows IoT.

What makes node great for embedded devices is that it is asynchronous, enables tight/small applications, has a rich ecosystem for extensions and offers built-in web services. It’s trivial to set up a RESTful API and web server, which both come in handy for talking to and managing devices.

For Windows on Arm (WoA), like the Raspberry Pi 2 (RP2), Microsoft’s implementation of node.js is based on the Chakra Javascript engine. As a result, there is a different process to get node.js on a Windows IoT device, as compared to downloading and building node for a Linux target device.

In the following steps below, we’ll work towards running a Hello World node app on a Windows IoT-enabled RP2. I’ve used the Microsoft developer guide to run your first node application which in addition showing how to load node on a device, outlines how you might interface to the underlying Windows IoT system via a C++ extension.

Getting node.js on to your Windows IoT device

The first thing that we must do is download the “Node.js Tools for Windows IoT“, which includes the Chakra version of Node.js and tools to help manage node for devices. You may need to scroll down a bit to find the Node.js Tools, but once you do, run the installer to install this to your Windows development machine.

Install Node.js Tools for Windows IoT

Install Node.js Tools for Windows IoT

You’ll also need to download Python 2.7 for your Windows development machine. There are multiple versions available, but to be consistent with Microsoft’s documentation, I pressed the Python 2.7.X button.

With the necessary tools installed, you need to build and copy the Chakra node engine for the RP2. To do this you must first gain access to the device. Use Windows Explorer and enter:

\\<IP Address of the RP2>\C$

You will be prompted for the username (<IP Address of the device>\Administrator) and password (p@ssw0rd). This step is important because it allows your development machine to have access to the necessary target locations on the RP2 where node is to be loaded.

Access Windows IoT device

Access Windows IoT device

With that in place, we’re ready to build and load node.js. To do this, we’ll run a PowerShell script called CopyNodeChakra.ps1, using the following command:

& 'C:\Program Files (x86)\Node.js (chakra)\CopyNodeChakra.ps1' -arch ARM -ip <IP Address of the RP2>

If the security policy limits successful completion of this command, open PowerShell as Admin or run via a PowerShell ByPass. If successful, node will be copied to the C:\Node.js (Chakra) directory. (In the picture below you can see that I used the ByPass to complete the script.)

Copy Chakra to IoT Device

Copy Chakra to IoT Device

To verify that node is properly installed, you can start a PuTTY SSH session to the RP2 and navigate to the install location then issue the familiar node and npm version commands:

Check node and npm versions

Check node and npm versions

Run Hello World on a Windows IoT RP2

With node on the RP2, we’re ready to run our hello world application. I copy-pasta’d a very simple application using a text editor, but if it’s a more complex application, you can use Visual Studio to manage development. Since I had a Windows Explorer session opened, I just copied my hello.js application to the RP2 and put it in the directory in which node was installed – of course, as the node application matures, you’ll want to create a separate deployment directory.

Hello World node application (Hello.js)

Hello World node application (Hello.js)

Before we run the application, we need to ensure that the node.exe can communicate through the firewall. We’ll need to connect to the device, using PowerShell and issue the following commands:

' Start the WinRM service for remote connections
net start WinRM
' Set as trusted host
Set-Item WSMan:\localhost\Client\TrustedHosts -Value <IP Address of the RP2>
' Initiate a session with the RP2
Enter-PSSession -ComputerName  -Credential <IP Address of the RP2>\Administrator
' With the remote session established, allow the firewall exception for node.exe
netsh advfirewall firewall add rule name="Node.js" dir=in action=allow program="C:\Node.js (Chakra)\Node.exe" enable=yes

Finally, we’ll use our PuTTY session to run our node Hello World application and then connect to our Windows IoT device using a browser to see the results:

Hello World on Node for Windows IoT

Hello World on Node for Windows IoT

Advertisements