Somebody Went and Made Node.js All Easy! o_O

I get busy learning some other things and Node.js gets all easy! I’m impressed. 🙂

1. Navigate to the main node.js site, there’s a big “Download” button that you can’t miss.

The Node.js Site, Just Click It And Get It...
The Node.js Site, Just Click It And Get It...

2. Click that download button and you’ll be presented with this crazy easy screen to pick your download.

Node.js Download Link...
Node.js Download Link...

3. Once you get that installed, get nodeunit. You are writing unit tests against your node code right? 😛

[sourcecode language=”javascript”]
npm install nodeunit
[/sourcecode]

Results from loading nodeunit with npm install.
Results from loading nodeunit with npm install.

For more on how to use npm, which stands for Node Package Manager, give a RTFM. 😀  It’s kind of like Nuget for .NETters and Gems for Rubyists.  🙂 …and trust me, I’ll be putting this to use in future blog entries and it’s a big help to have handy.

4. Create a file to code some javascript in. Per the simple example given on the main node site…

[sourcecode language=”language"javascript"”]
var http = require(‘http’);

http.createServer(function (req, res) {    res.writeHead(200, {‘Content-Type’: ‘text/plain’});
res.end(‘Hello World\n’);}).listen(1337, "127.0.0.1");

console.log(‘Server running at http://127.0.0.1:1337/’);
[/sourcecode]

Now run this. If you have the Webstorm IDE from Jetbrains you can just right click on the js file and select run. It automatically will use Node.js to execute your JavaScript. You can also execute node.js by opening up a shell/terminal and executing this.

[sourcecode langugae=”bash”]
node whereverYourPathIsTheFileIsLocated/example.js
[/sourcecode]

Navigate to 127.0.0.1:1337 and you’ll get the classic “Hello World” message.

Running The Node.js Example.js File
Running The Node.js Example.js File

…and all of a sudden you’re using node.js to hack some javascript! Seriously, there aren’t many usable languages out there that are as code ready as javascript with node. Very cool, very very cool.

Happy coding!

Service & Scheduler: Using Topshelf, Quartz, & Other OSS Bits Part 1

This how-to entry will detail the steps for getting Topshelf installed, running, and a schedule integrated and timed appropriately using Quartz. Once that is complete I’ll go over how to get custom schedules added to the service.

Topshelf

To get Topshelf up and running open up Visual Studio, start a Windows Console Project, and then use Nuget (if you don’t have Nuget installed, I suggest doing that ASAP, you’ll need it to follow along with any reference additions).

Get Topshelf Through Nuget
Get Topshelf Through Nuget

Once you have Topshelf and associated references added via Nuget create a new class file called StatusCheckIn and add the following code.

[sourcecode language=”csharp”]
using System;
using System.Timers;

namespace Sample.Services
{
public class StatusCheckIn
{
readonly Timer _timer;

public StatusCheckIn()
{
_timer = new Timer(5000) { AutoReset = true };
_timer.Elapsed += TimeHappened;
}

static void TimeHappened(object sender, EventArgs eventArgs)
{
Console.WriteLine("Status Time: {0} checking in.",
DateTime.Now);
Console.WriteLine("Sender Type: {0}",
sender.GetType());
Console.WriteLine("Event Argument Type: {0}",
eventArgs);
}

public void Start()
{
_timer.Start();
}

public void Stop()
{
_timer.Stop();
}
}
}
[/sourcecode]

After adding the StatusCheckIn Class, open up the Program.cs file and add or change the following code.

[sourcecode language=”csharp”]
using Topshelf;

namespace Sample.Services
{
class Program
{
static void Main()
{
HostFactory.Run(hostConfigurator =>
{
hostConfigurator.Service<StatusCheckIn>(
serviceConfigurator =>
{
serviceConfigurator.SetServiceName("TS");
serviceConfigurator.ConstructUsing(
name => new StatusCheckIn());
serviceConfigurator.WhenStarted(tc => tc.Start());
serviceConfigurator.WhenStopped(tc => tc.Stop());
});

hostConfigurator.RunAsLocalSystem();
hostConfigurator.SetDescription("Sample Host");
hostConfigurator.SetDisplayName("Display Name");
hostConfigurator.SetServiceName("Topshelf Service");
});
}
}
}
[/sourcecode]

If you run into an odd reference error, it is likely that VS2010 has determined you want the client profile of .NET 4.0 again. If anyone at MS on the VS2010/.NET 4.0 reads this, we DO NOT want this to be the default. It’s ridiculous that it gets set as default without some type of manual change. But I digress, open up the properties of the console/service project and set it under the dialog as shown.

Setting the appropriate .NET 4.0 Version in the Properties Dialog
Setting the appropriate .NET 4.0 Version in the Properties Dialog

NOTES: Working code available on Github under my Topshelf Repo.