Starting with nodejitsu.com

Node.js, an amazing new platform to create scalable network applications. It is sponsored by Joyent. Joyent on it’s turn, is a global cloud computing and systems software provider. They work together with Nodejitsu, to create the best node.js platform as a service or node.js hosting.

At the moment Nodejitsu is still in public beta, and you can create a free beta account on nodejitsu.com!

Hello world on nodejitsu.com

Using the interactive shell

For those who are not yet familiar with node.js, read & do this tutorial. There you will learn how to setup node.js and make a ‘hello world’ website. We are here, going to publish it on  Nodejitsu!

Install nodejitsu

Open your command prompt (cmd) by right clicking on it and choose ‘Run as administrator’.

Type this command:

npm install jitsu -g

When this is finished, you can enter your activation command:

jitsu users confirm <yourusername> <youractivationcodesendbyemail>

After this, your nodejitsu hosting is all set to go!

Deploy

If you want to put a node.js app or website online, you need to deploy it. (Wikipedia: Software deployment is all of the activities that make a software system available for use.)

Go to the location where you stored your hello world application (‘cd  C:\Users\elias\Documents\helloworld’).

Login  to Nodejitsu by typing: ‘jitsu login’. A step by step process will make you enter your username and password.

Now that you are logged in, let’s use the same methodology to publish our Hello World site. Nodejitsu also created a step by step batch for this. Enter this command in cmd:

jitsu deploy
App name Choose your app name (In my case createweb-helloworld).
Subdomain Choose your subdomain (subdomain.nodejitsu.com and subdomain.jit.su).
Scripts.start The start file of your node.js app. I choose main.js.
version <0.0.0> It will ask your version number. You can leave this empty or add something you like.
engines <0.6.x> This is the node.js version. You can leave this empty (implying you want the defaults).
Is this ok? It will ask you twice to respond with a yes (you can see a summary of what you entered).
error: 403 Authorization failed with the provided credentials. If you get this error, redo ‘jitsu login’, and then redo ‘jitsu deploy’. Now deployment should go fine.

Now you should be able to visit your site running at your choosen subdomain. Also, when you go to develop.nodejitsu.com, you will see your app!

Good luck with deploying your app!

Sources

  • https://github.com/nodejitsu/handbook/blob/master/chapters/hello_world.md

Node.js getting started tutorial

In this tutorial I am going to guide you around in working with Node.js!

  1. Get started with node.js
  2. Create a hello world app
  3. Use modules

Get started with node.js

Note: This tutorial is assuming you use Windows as your operating system.

Go download the latest Windows Installer. Install it.

You can go the node console by starting cmd, and entering node. Test if it works by typing: “console.log(‘Hello world’);

You can get out of it by typing Ctrl + C twice.

Let’s try to make our first ‘Hello World’ application!

  1. Choose a location on your hd where you want to develop your websites.
  2. Choose an IDE (Integrated Development Environment), you could just use a text writer, or any program you like. I am going to use Eclipse EE, since I am used to working with it.
  3. Create a file ‘hello_http.js’
var http = require('http');

var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello Http');
});
server.listen(8080);

Now in cmd go to the folder where you saved the file, start node (by typing: node hello_http.js).

If you now go to your browser (http://localhost:8080), you should see:

What does this very short script do?

var server = http.createServer(function(req, res) { res.writeHead(200); res.end(‘Hello Http’); });Resources

  • http://nodejs.org/
  • http://nodeguide.com/beginner.html
var http = require('http');
Include a node.js core module and make it accessible through the variable http.
var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello Http');
});
Create the server. This method gets executed everytime a request comes in.

The “req”, is the request, this can contain form data, or files (upload).

The “res” object is a writeable stream to send data back to the client.

server.listen(8080);
This makes our server listen on port 8080.

Organize your app with modules

Node.js uses modules to organize your sites. Just like how you can include core modules, you can create your own modules. Below is an example that creates a ‘Hello’ module, used to return the famous ‘Hello World’.

Hello.js:

exports.world = function() {
  return('Hello World');
}

The exports.* is the object used to return methods to the scripts that use this module.

Then create another file, Main.js. This will be our start point. The difference is now, that it will use our Hello.js module, to return the ‘Hello World’ string.

Main.js:

var http = require('http');
var hello = require('./hello');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end(hello.world());
});
server.listen(8080);

As you can see we can include hello, with a ‘./’ before (means that the file is in the same folder), and without the extension, because node automatically assumes it is a .js file.

There is more!

This should cover the basics. If you want to do more, try to understand how EventEmitters work in node.js!

I, am currently reading and playing around with expressjs!

Resources

  • http://nodejs.org/
  • http://nodeguide.com/beginner.html
  • http://expressjs.com/

What & why node.js

Node.js, what is it??? Node.js is a software system designed for making brilliant webservers that can host scalable (rich) internet applications in an event-driven way.

Why event driven (versus thread-based)?

What do we mean with this? The webservers we are used to know, like Apache, IIS are thread-based. As soon as you start communicating with them, you have your own private communication line with the webserver. Nobody else is allowed to use that one, until you are completely done. In comparison, node.js & nginx (Thanks Bradley Wright for pointing this out), as soon as it received your request, doesn’t wait for the request to be completed and send back. It stacks the request on a queue, and goes to the next request, from someone else. So a user requesting something, is an event. Node.js stacks them up, and each them when it finished a request, will reestablish contact with the client again.

If this doesn’t make sense to you, please read Dan York’s blogpost.

Scalable?

An open connection between a client and a node.js application, only requires minimum memory / cpu (server-side). A test by Matt Ranney from Voxer actually made clear that they sooner ran out of ports, than cpu or memory. This means that when having about a bit less than 65535 open connections, the node.js server still continued working, without remarquable lag.

Rich internet applications?

These websites, are applications that formerly only used to be a program. Like a Calendar, Word, … . Thy process your input, and update what you did without the pages refreshes. They are connected with other users. And what you see could actually change, when somebody else changes something, somewhere else on the internet.

Javascript V8?

To create something in Node.Js, you have to write a program. That program, can contain the code for the actual web server implementation. The code, has to be written in Javascript. Node.js use Javascript, because it can use V8, the (open source) engine from Google, to run/handle it.

When should you use it?

When should you not use it?

  • You do not want to learn something new.
  • Websites.
  • Very complex thingss.
  • If it is not deployable.
  • If it has to be real stable (still very young platform)
When should you use it?

  • If you have many connections.
  • If it has to be very quick, like desktop software.
  • If your client code already is in Javascript.
  • If you use push apis.
  • Transcoding.
  • Real Time data

This table is a summary from an openminds slideshow.

What is next?

I am going to write a tutorial about setting a Node.js server up, about making a hello world application and something more complicated.

Thanks & comments!

Thanks for reading. If there are any mistakes in this articles, please comment! If you have anything to add to this, please contribute!

Sources:

  • http://code.danyork.com/2011/01/25/node-js-doctors-offices-and-fast-food-restaurants-understanding-event-driven-programming/
  • http://www.sdtimes.com/content/article.aspx?ArticleID=35668&page=1
  • http://www.slideshare.net/frank_be/nodejs-waarom-en-hoe (dutch presentation)
  • http://www.theregister.co.uk/2011/03/01/the_rise_and_rise_of_node_dot_js/