Install Node.js on Windows

Install Node.js on Windows

Node.js is a lightweight yet extremely powerful open source, cross-platform runtime environment for hosting and running JavaScript applications. Its popularity continues rising alongside with the popularity of JavaScript. The way we are able to develop web applications today has changed drastically from only a “few years” back where JavaScript could only be run in the browser. Today we are able to build big and complex applications in JavaScript and host them in Node.js, where in the past these types of applications had to be written in languages like Java, PHP, ASP, etc.

It’s very easy and fast to get going with Node.js which I’ll demonstrate.

1. First off download the 64bit Node.js msi-installer

2. Follow the installation and install Node.js to the default location (C:\Program Files\nodejs)
Make sure npm package manager is installed as well, as you will need this later on.
Also let Node.js be added to PATH (default), so you can access it from anywhere.

Node.js install on Windows

That’s it!
Open up Command Prompt and try the following:

node -v

Node will tell you which version it’s running:

Node.js version

As you can see, I’m running v.4.4.5 of Node.js

Go ahead and try the following as well

npm -v

Node.js npm version

It displays I’m running npm package manager v.2.15.5

We are now confident that both Node.js and npm are working and we can build our first application.

1. Create the directory C:\Node
2. Create the file “hello.js” in C:\Node with the following content:

[javascript]console.log("Hello World!");[/javascript]

3. In Command Prompt go to C:\Node and run;

node hello.js

Node.js hello world

It’s pretty basic, but we build and ran our first JavaScript application in Node.js.
Now let’s try to expand and host a web application.

1. Create the file C:\Node\server.js and paste the following;

[javascript]
//Include the http module
var http = require(‘http’);

//Create a webserver
http.createServer(function (req, res) {

//Respond to any incoming http request
res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World!\n’);

}).listen(1337, ‘127.0.0.1’);

//Log that we started listening on localhost:1337
console.log(‘Server running at 127.0.0.1:1337’);
[/javascript]

2. Standing in C:\Node in Command Prompt start your web application;

node server.js

Node.js server

3. Go to http://localhost:1337/ in your browser (from the same machine running Node.js of course)

Node.js browser

We’ve now created a JavaScript application and hosted it through http with Node.js.
It’s still pretty basic, but the possibilities are endless. For example Ghost – a blogging platform like WordPress, and actually created by one of the former WordPress developers, is a Node.js application.