My First Node Application My First Node Application

I have the unique pleasure of being a technical reviewer for an upcoming book by a former co-worker of mine.  Since it will be his second book on Node (his first being a co-author of Node Up and Running), I thought I should probably get a feel for using Node.  I've of course read some of the documentation and have a thorough understand of JavaScript already, as well as event driven programming – working on some very large MMO projects.

I thought I would share how easy it is to really and truly get up and running with Node.


I think the longest part of this process was installing Node itself!  To start, you'll of course need to install the Node platform on your computer or server.  Do this by visiting Nodejs.org – there was a nice big Install button ready for me to download the msi version and install on my Windows box.

Once installed, I wanted to make sure that Node was working properly on my machine.  I validated this by opening up a command prompt (Start -> Run -> cmd).  Inside the command prompt I simply typed node.  Since I didn't get an unknown command and actually received a Node command prompt I was fairly confident that I was ready to go.

Since I'm a fan of MVC frameworks, it's only logical for me to want to use and leverage Express.  Express takes some of the pain out of the way when you're starting with a brand new HTTP server – like Node.  Without express, I would need to spend time doing my own routing and directly input to the correct functions, etc…  Seems like a bit of a pain to me; hence where Express comes into play!

The beautiful part about this (for me specifically) is that since my new blog is running on the Slim PHP framework, routing looks very similar!  Yey, less of a learning curve.

Since Node now has its own built-in package manager, installing Express couldn't be simpler.  Inside of our command prompt again:


> node
> npm install express

Let Node Package Manager (NPM) do its work and Express will be ready.

So far this has taken me probably less than 3 minutes – much more time to write this actually blog!

Now that I have Express installed, I can begin creating my first Node application.  Create a new file (I called mine first.js):


var express = require('express');
var app = express.createServer();
app.get('/', function(req, res) {
res.send('Hello');
});
app.listen(8000);

Express takes care of everything for me, I just had to send a result back to the client when they connect to the root of my set ('/').

Back in the command prompt I need to start the Node application:


node first.js

My Node application is now running.  I can visit in a browser by visiting http://127.0.0.1:8000.  Notice the port 8000 as it matches what I'm using in the app.listen function above.  I can't use 80 on my local machine as I have IIS currently running on that port.

If you are running this code right now, you might receive the following message inside of Node as I did:


Warning: express.createServer() is deprecated, express
applications no longer inherit from http.Server,
please use:
var express = require("express");
var app = express();

Looks like Express and Node are being actively updated and my code reference is a bit out of date.  To solve this, you can update your code as follows:


var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello');
});
app.listen(8000);

Bored?  Me too, but the goal of this blog was to test how difficult or should I say how simple it is to create a Node application – all-in-all I would estimate my development time at under 5 minutes!  The fun has only just begun…

Published on Oct 4, 2012

Tags: Node js Tutorial | JavaScript | express

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.