Search Courses

Search Courses
Home / Blog
node.js installation and hello world - easy steps on eclipse

node.js installation and hello world - easy steps on eclipse

Posted on : 04,Jan 2014 2970

node.js ,express js and eclipse

node.js has become a real big deal because of its uniqueness, Node is extremely useful in building fast, scalable network applications, because of its capability of handling a huge number of simultaneous connections with high throughput, means high scalability.

As Wikipedia says: “Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.” Beyond that, it’s worth noting that Ryan Dahl, the creator of Node.js, was aiming to create real-time websites with push capability, “inspired by applications like Gmail”. In Node.js, he gave developers a tool for working in the non-blocking, event-driven I/O paradigm.

I am trying to show how to install node.js and run a simple code on it. Got tonnes of information from blogs and tutorials and thought of putting together the easiest way of doing it. For any queries you can post a comment on this knowledge share and I normally check and respond without much delay.

First thing first

Get all needed tools and software. This makes your life easy.
  • node.js ofcourse - install it from node , the site detects your OS and recommend the installable.
  • eclipse - you can use editor of your choice . but eclipse makes your effort reduced to half , especially if you need to upload / download to github or any PaaS environment like bluemix or azure etc . I installed Eclipse Luna SR1a (4.4.1) from eclipse download

Configure your way

  • Install node.js - install it from the download you did on previous step.
  • Open a command prompt and install some quick and handy tools for our node.js platform
Expressjs is a very minimal and flexible node.js web application framework.
$ npm install express --save 

$ npm install body-parser

$ npm install method-override

bodyparser is Node.js body parsing middle-ware and methodOveride lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
  • Install eclipse and configure marketplace - After eclipse installation , goto help>Eclipse market place.
You can install some plug-in to write quick node.js code ,search for Nodeeclipse core in market place and install.

Bear the fruits

Open eclipse ,open new project and choose Node.js express project as in below image.

Name your project and choose template engine , this is the engine which render the view, I chose jade and stylesheet engine as css.



Eclipse will take care of rest , it will give you a project folder structure , with routes and views and heart of your code app.js. You can add new folders if you want to code in typical MVC way. I chose not to , at least in my first trial code.



This is a typical express js project folder structure.
  • routes - holds controller files - which help in routing between the files. It also has model files, which do back end database querying if needed.
  • views - hold view file i.e. jade files which shows up on the browser.
  • app.js - is the heart of your code, it has initializations , declaration , setting dependency , creating server etc.
  • public- holds static files like images, css etc
Let us bisect app.js code . By default app.js code is as below ,

/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});

it has dependency module which set some some variables
app.set('port', process.env.PORT || 3000);
set the port default to 3000 , you can change it , if you want to run your code on some other port. I am changing into 3001
app.set('port', process.env.PORT || 3000);

app.use(express.favicon());
set favicon, you can remove it ,if favicon is not needed
app.use(express.logger('dev'));
this is used for debugging purpose , can write to console using console.log ,useful while you are debugging.
app.use(express.bodyParser()); 
app.use(express.methodOverride());
the above snippet used for parsing elements in the html body and for method overriding
app.use(express.static(path.join(__dirname, 'public')));
this set a static path for your files. for e.g if your css or images etc are on folder public then you can use /static/css_file.css in your jade files.
routes = require('./routes');
user = require('./routes/user')
the above code sets your router variable.
app.get('/', routes.index);

app.get('/users', user.list);

and this helps in url routing during "get" to route you to different pages.
eg. user.list is nothing but a function inside /routes/user.js file , where list is the function name

Now lets start the server
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
We are all set to start the server , in eclipse what you need to do is just "Run as" node application


You can see server started and running log on the console below


One thing to keep in mind is that any change in your .js files, a server restart is required to reflect the changes. But if the code change is in .jade files not restart is required.
You can either terminate the server in apache as in below image.



or you can install nodemon
$npm install nodemon
nodemon monitors the files and restart the server automatically if there is a file change. cool is not it !
$ nodemon server.js
if the script is omitted, nodemon will try to read "main" from package.json and without a nodemon.json, nodemon will monitor .js, .coffee, and .litcoffee by default.

check out the site on http://localhost:3001/

Let us change something and do a "Hello world". Goto routes/index.js and make it as below.
exports.index = function(req, res){
res.render('index', { title: 'Hello world' });
};
I am passing title a variable as 'Hello world' and rendring index.jade file , index.jade is the file which is displayed in your browser screen. You can style it adding css on head portion.
link( rel='stylesheet',href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/ css/bootstrap.min.css')
You may have the head portion in layout.jade, this is for easy and fast rendering, you can keep your style and functionality specific objects separately.
That is it, we are done with our first "hello world" program on node.js without much "coding".


Join the discussion

Improve the thread with your input and questions.
Carl Usmov
Consultant at IT Synergy Inc

Member Since : Dec, 2012

San Francisco California

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable networ

Take course on


Interested in node.js? We will notify you when knowledge is shared.