共用方式為


Node.js on Nano Server

One of Nano Server’s core scenarios is to serve as a lightweight OS for born-in-the-cloud applications running in a VM or a container. Nano Server already supports ASP.NET Core (aka ASP.NET 5) with IIS, and now, we’re happy to announce support for Node.js.

The instructions in this blog explain how to get Node.js running on Nano Server. If you want to try this on a Nano Server VM, with Windows Server 2016 Technical Preview 5 there are three ways you can quickly get a VM:

  1. Download the developer VHD at the above Technical Preview 5 link
  2. Create a VM in Azure using the Nano Server image in the Azure Gallery. If you don't have an Azure account, you can get a free 30-day trial
  3. Download the Technical Preview 5 ISO at the above link and then consult the Nano Server documentation to build a VM.

 

Installing Node.js on Nano Server

Download Node.js to your development machine from nodejs.org (I downloaded the LTS version) and follow the installation instructions to install Node.js.

Once installed, use the following PowerShell script from an elevated PowerShell console, classic or ISE, to copy the binaries to your Nano Server machine:

$ip = "0.0.0.0" # the IP address of your Nano Server machine

$s = New-PSSession -ComputerName $ip -Credential ~\Administrator

$nodePath = "C:\Program Files\nodejs"

# this is the default node.js folder - change it if you specified a different folder during installation

Copy-Item -ToSession $s -Path $nodePath -Destination "C:\" -Force -Recurse

 

Setting up your environment and running “Hello, world!”

Remote into your Nano Server machine:

Enter-PSSession $s

Now add the node.js folder to your path environment variable:

$env:path += ";C:\nodejs"

To make this change persist even after your Nano Server machine reboots, enter the following:

setx PATH $env:path /M 

Quick test:

node --version

You should see the node version. Now, exit out of your Nano Server machine:

Exit-PSSession

Back on your development machine, use your favorite editor (I use VS Code) and create a JavaScript file (hello.js) with the following content:

var util = require("util");
console.log("Hello, world!");

Save the file and copy it to your Nano Server machine:

Copy-Item -ToSession $s -Path ".\hello.js" -Destination "C:\" -Force

Remote into your Nano Server machine again, and run node:

Enter-PSSession $s

node c:\hello.js

Congratulations on running your first node.js app on Nano Server :)

 

A Note about Native NPM Modules

On Nano Server, you cannot use npm install to download and compile native modules, such as bufferutil or node-sass, because the Visual C++ compiler doesn’t run on Nano Server due to its .NET framework dependency. Instead, you should use npm install on your Windows development machine to download all your dependencies, build your app, and then copy your entire project folder to Nano Server, along with all other npm modules and the C++ redistributable binaries.

You can download the C++ redistributable binaries from the Visual Studio site. Click on “Tools for Visual Studio 2015” on the left, then click on “Microsoft Visual C++ 2015 Redistributable”. Make sure to select x64 before you click “Download”! Follow the installation instructions.

Once installed, run the following PowerShell script to copy the redistributable binaries to your Nano Server machine:

$files = "concrt140.dll", "msvcp140.dll", "vcamp140.dll", "vccorlib140.dll", "vcomp140.dll", "vcruntime140.dll", "mfc140.dll", "mfc140chs.dll", "mfc140cht.dll", "mfc140deu.dll", "mfc140enu.dll", "mfc140esn.dll", "mfc140fra.dll", "mfc140ita.dll", "mfc140jpn.dll", "mfc140kor.dll", "mfc140rus.dll", "mfc140u.dll", "mfcm140.dll", "mfcm140u.dll"

foreach ($f in $files)

{

    Copy-Item -ToSession $s -Path $env:windir\system32\$f -Destination $env:windir\system32

}

 

What's Next?

Node.js is the first in a series of app frameworks we will document how to get running on Nano Server. Let us know what your favorite app framework is, and be generous with your feedback on your Node.js experience on Nano Server.

Happy coding :)

 

ref@