Archive | Nodejs RSS for this section

Nodejs Sqlite Module Issue

I have been trying on setting up with nodejs sqlite module (Node-Sqlite) and i am facing an issue as follow when i try to build

node-waf build

it throws me following error

Waf: Entering directory /home/anand/Downloads/node-sqlite/build'
[1/2] cxx: -> build/Release/sqlite3_bindings_1.o
../ fatal error: node_events.h: No such file or directory
compilation terminated.
Waf: Leaving directory/home/anand/Downloads/node-sqlite/build'
Build failed: -> task failed (err #1): 
{task: cxx -> sqlite3_bindings_1.o}

My platform was ubuntu v11.04 and node v0.6.15

i found an another module to over come this issue node-sqlite3

installation can be done pretty simple with npm registry

npm install sqlite3

Static Website With node.JS

I am really addicted to nodejs stuffs and modules, not just because of javascript; but also because of a few videos and blogs that I read, made me spend time with learning nodejs. I am at the beginning stage of learning and exploring nodejs. Hence, I am putting everything I have experienced here.

My previous article “Introduction to Nodejs” helps you install and start programming on node platform. This time, I am going to help you create a simple static website on top of nodejs and how to use node modules. This static site can be done with simple HTML and CSS. However, in my opinion, learning in practical manner will add more value in lesser time than reading lots of theories. The following article helps in your Node learning curve rather than a real world problem solver.

Let me take a simple use-case before getting started. I want a simple Shopping cart app built on top of nodejs I will be using this use-case throughout my series of articles.

Why Nodejs?

  • I want my application to serve concurrent requests at same time
  • I want my application to run on distribute environment
  • Javascript is my favourite language and Nodejs is much similar to Javascript

Lets first install Pre-Requisites!

Node Package Manager(npm) is a package manager for node. You can use it to install and publish your node programs. It manages dependencies automatically and can be lot more useful. To install npm, use the following command:

curl | sh

To verify the installation, type the following command:

npm –v

This will print the version of npm installed. As of now, npm is supported only in Linux. We can make use of the npm to install node modules that we need. There are a bunch of node modules we will be using for our site

  • Express, a web framework
  • EJS, a template module
  • LessJS, a styling module

Lets me explain those modules in detail in my upcoming articles. Now, we can just install them by using the Node Package Manager(npm).

npm install express
npm install ejs
npm install less

Getting started

We need a simple page which should display “Coming Soon!” message. The URL for the site will be http://localhost:3000

First we need to run a webserver that will be reached at http://localhost:3000

I just created a folder named “Node_Shop” to place all my code for this project. Create a file server.js, where we can start a server with express and made it to listen on port 3000.

var express = require(‘express’); //This will include expressjs module that we have installed.
var server = express.createServer(); //creating express server
res.set(“hello world”);
server.listen(3000); //will make server listen at port 3000
console.log(“Running at http://localhost:3000”);

The server.js can be executed as:

node server.js

When we hit the url http://localhost:3000 in browser, we will end up with a hello world display!

Now, create a folder “views” under Node_Shop, where all our layout files get stored. After creating the folder, edit server.js and set the path for our template and specify the template engine(in this case, it’s ejs).

server.set(‘views’, __dirname + ‘/views’); //specifying template path
server.set(‘view engine’, ‘ejs’); //specifying template engine

Lets add our view files layout.ejs,and index.ejs. The ejs extention is nothing but a template page. There are many template modules in nodejs, like Jade, haml, etc., that can be used with express. The layout.ejs contain common layout that all pages have. A simple layout.ejs for our site is given below:

< head>
<title>Node Shop</title>
<%- body %>

In this, is where the content from other view pages will fit. To create a “coming soon” page as the default index page, edit the index.ejs and add the content given below:

<h1>Coming soon</h1>

Lets reset our get response to render the index.ejs by using the render function.


The template engine(ejs) will smartly render the index.ejs by combining both the index.ejs and layout.ejs as a single template. After running the server, in the browser, do a right click and view the source. We can see the combined template page.

<title>Node Shop</title>
<h1>Coming soon</h1>

In this article, I am not going to compile lessjs via express server. I am going to do it via normal include of file less.js on head.

Create another folder “static” under Node_Shop where we can store all the static files such as style sheets, images and javascript files. Place a “main.less” file with your desired style within the folder. Finally, we shall style the page with lessjs by including *.less files within the head tag.

server.use({ src: __dirname + ‘/static’ });
server.use(express.static(__dirname + ‘/static’));

and the fully rendered HTML page

<title>Node Shop</title>
<link rel=”stylesheet/less” href=”/static/main.less”>
<script src=”/js/less-1.1.3.min.js”></script>
<h1>Comming soon</h1>

Lessjs is something to add greatness to the styling. We can either include the *.less files within the head tag or we can compile the files via express server itself. I will explain lessjs briefly in the later part of this series.

The complete code can be found on my git-hub repository

Thanks and I hope this article might help some one new to nodejs

Introducing node.JS

In general node.JS is a bunch of sugar on top of very complex virtual machine (V8) written by Google (V8 is a JavaScript engine inside of chrome) .It is a beast, it’s a huge, high performance virtual machine written by smart people .That’s not what node is.Its a set of libraries written on top of V8 that uses its greatness to do networking.

Installing node.JS

Just follow these steps for Unix-like platform.

$ wget
$ tar xzvf node-latest.tar.gz
$ cd node-v0.4.7
$ ./configure
$ make
$ sudo make install.

This in general is enough. After all that you can write the node command in your terminal. However if there are some problems while compiling it, you may refer to Git and the Node community for more help. Don’t forget that V8 can work only on x86, x64 and ARM architectures.

Interactive node.JS shell

If everything worked, you should be able to invoke the interactive node.JS shell ,you type node without arguments you get this prompt just as python and you could type real JavaScript in that

$ node
> 2+4

Node has a rebuild and you could type functions

> function add(a,b) { return a + b }
> add(5,4)

Now let’s just write a simple hello world program on node, that’s where we all start. Obviously you open up your text editor, I use vim.

$vim hello_world.js

e.g. helloworld.js

setTimeout(function() {
}, 2000)

What this code does is…. let’s try, It prints hello and then waits for 2 seconds and then prints world. And then it exits.

Here what does setTimeout do?

SetTimeout has two arguments, First is call-back, and then a number of milli seconds you want it to wait.

Something that I need to say is,node should be somewhat familiar for JavaScript people.People who are familiar with browser side JavaScript should feel comfortable, not just for javascriptness but for the same pattern of coding.

The same helloworld.js can also be written in PHP as follows:-

e.g. helloworld.php


What is the difference between code in helloworld.js and helloworld.php?

In helloworld.js the hello happens immediately as it happened right through in helloworld.php we kind of stopped on the sleep.That is the fundamental difference.In node you never ever sleep, never have you fetched a URL from website and wait for it to come back.

You don’t have the ability to sleep on node and there is no possibility of halting executions, you can set Timeout to be ideal. In helloworld.js,it is not that CPU is spinning in busy loop,the CPU goes to zero and ideal and the OS unschedule’s it and schedules it back after timeout.Node is good at idling and it doesn’t sleep though.

Let’s improve on this example

What if i change setTimeout to setInterval?

e.g. helloworld2.js

setInterval(function() {
}, 2000)

It prints

>and so on ..

This brings a important property of node:-it exits when there is nothing left to do. It has a natural ability to reference count the callback or intervals, it knows there is something to do, so it doesn’t dropout or once all gets done, it dropsout automatically.

These are some basic concepts of node.JS.