“Hello, World!” with Node.js on Windows, Mac, and Pi

So, we’ve all heard of Node.js by now. I assume you had the same reaction I did, which was:

“Hahaha, that would be hilarious! Writing a server in JavaScript!! Oh man, what a horrible language….. what do you mean you’re serious? What do you mean tons of people are using it? I thought this was a prank, a joke??!”

Well, it’s not a prank, Node.js is here to stay, so I guess we all need to start coming to terms with it. So, here’s a crash-course, as I understand it, which might help you get started.

What is Node.js?
Node.js is a JavaScript run-time which you can run on many/most platforms. It was written in C++ by Joyent, and it’s primarily used to create networked applications. For example, you can create a “lightweight” web/HTTP or TCP server. That is, a server that responds to HTTP or TCP requests.

Where do I get it / how to install?
Navigate to http://nodejs.org/ and click the download link. There are installers for Windows, Mac OS, Linux, SunOS, and there is source for you to compile on any other platform. See below for how to install on specific platforms:

Installing on Windows:
Navigate to http://nodejs.org/download/ and choose the .msi installer. Just follow the wizard, as you might expect:

image

It wants to install it in C:Program Filesnodejs (this is on a 32-bit VM) – and this will set the path, install “node package manager” (NPM), similar to NuGet – but for Node.js packages:

image

image

and sure enough, if I create a little web server (source of code from here):

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello Worldn");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

Then, I start the web server with:

node nodetest.js

You’ll see output like the following, and I can connect to port 8000 in a browser:

image

 

Installing on Mac OS:
The story is very similar here. Navigate to http://nodejs.org/download/ and download the .pkg file. Run the wizard and follow the prompts:

image

It tells you where it’s going to install it:

image

Then, again – using code from here, I create a test server file in my home directory:

$ pico ./nodetest.js

and then paste in the following (code taken from here):

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello Worldn");
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);

// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

then, to run the server, I type:

$ node ./nodetest.js

I now have an HTTP server running:

image

and sure-enough, I can connect to port 8000 with a browser too:

image

 

Installing on Raspberry Pi:
This is one of the few places where Node.js makes sense! This is quite easy, if you are using Debian-based Linux distributions – you guessed it, you can install it like this:

$ sudo apt-get install nodejs

The bad news is this installs the much older version – v0.6.19, where 0.12.0 is the latest. Anyhow,  I can still do the same thing, create my nodetest.js file, start it up with:

$ nodejs ./nodetest.js

and then connect to it and get a response:

image

Using Node.js and NPM, once installed:
Node.js has a “package manager” for installing components. This is starting to become the norm for many technologies. This is like an App Store, but for components. This is similar to NuGet (.NET), Gems (Ruby), CRAN (R), Maven (Java), etc, etc. NPM is Node Package Manager.

If you followed the above, NPM is likely already installed. Some of the basics you’d run from the command-line:

  • npm help – brings up help
  • npm find keywordhere – looks in the repository for keywordhere
  • npm install packagename – installs the specified packagename
  • npm update – updates existing packages to the latest version

Bottom line:
It hurts my soul a little bit to have to write this blog post. Why am I so anti-Node.js? Primarily because we, as an industry, know better. We know WAY better than this. We knew back in the 1995 that JavaScript was a horrible language. It had all of the worst features, and virtually none of the best features that we learned were good! But, because it ran on everything – it gained momentum.

Now, 20 years later, instead of migrating to a smarter language, or fixing this one – we continue to throw good money after bad. It just a waste of our time. It makes me sad that our industry is that lazy, or ignorant, or uninformed, or whatever – to not throw JavaScript into fire and let it die. Not only are we not doing that, we are building MORE things with it – like Node.js. Ugh. What’s so messed up about Node.js is the “idea” is clever – a lightweight networking framework and language. That is the clever part, too bad that JavaScript is the language that is what was used. Why don’t people do the same thing, but with proper languages – like F#? Or even Python?

OK, I said we all need to come to terms with this, Node.js is real and it’s not leaving. I clearly, have some more “accepting” to do!  🙂

Anyhow, all that aside – in my opinion, it is a clever little solution for these small-form-factor installs: like Raspberry Pi, Arduino, Gadgeteer, etc. That is likely how I’d use this. If you weren’t familiar, hopefully this helps get your feet wet – because it looks like Node.js will be around for a while.

Posted in Apple Stuff, Computers and Internet, General, Infrastructure, Linux, New Technology, Raspberry Pi, Uncategorized, Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 4 other followers

%d bloggers like this: