Angel \”Java\” Lopez on Blog

January 31, 2011

Playing with Node.js, Ubuntu, Sqlite3 and node-Sqlite

Filed under: JavaScript, NodeJs, Sqlite, Ubuntu, Web Development — ajlopez @ 10:42 am

I was playing with Node.Js, the Javascript library that can be used to create web server applications. Because it could be a nightmare to install it under Windows, I installed it at Ubuntu. I downloaded the code from:

http://nodejs.org/#download

After expanding the file, I switched to the directory and execute:

./configure
make
sudo make install

(Note that is a bit different from one of my sources Learning Server-Side Javascript with Node.js)

Then, I installed Sqlite3 in my Ubuntu box (not Sqlite, the node-sqlite library uses Sqlite3):

sudo apt-get install sqlite3

In a worker directory, I executed:

sqlite3 test.db

This a command line tool (no server to start, it goes directly to a file just created, named test.bd). Then, I enteder:

create table customers(id int, name varchar(30), address varchar(30));
insert into customers(id, name, address) values (1, ‘Customer 1’, ‘Address 1’);
insert into customers(id, name, address) values (2, ‘Customer 2’, ‘Address 2’);
insert into customers(id, name, address) values (3, ‘Customer 3’, ‘Address 3’);

My creativity for test data is proverbial 😉

There is a list of modules for Node.js:

https://github.com/ry/node/wiki/modules

I wanted to use express node.js module, then I downloaded from Express. Expanded it under my worker directory, as express. In that directory, I executed:

./install.sh

Now, I downloaded the source of node-sqlite from grumdig github. (I should try the other implementation: http://github.com/orlandov/node-sqlite).

I expanded in a subdirectory of my work directory, switch to such subdirectory, and try the installation instructions:

node-waf configure
node-waf build

But the second one failed. The build of the c++ binding to Sqlite3 required the source code of the database. So, I go again to:

sudo apt-get install libsqlite3-dev

and try again. All OK.

After some mini examples, I wrote a simple web app:

/**
 * Module dependencies.
 */
var express = require('./express/lib/express');
var sqlite = require("./node-sqlite/sqlite");
var sys = require('sys');
/*
 * Open the database
 */
var db = sqlite.openDatabaseSync("test.db");
/*
 * Creates the web server
 */
var app = express.createServer();
app.get('/', function(req, res){
  res.send('Hello World');
});
app.get('/customers', function(req, res){
  res.writeHead(200, { 'Content-Type': 'text/html' });
  db.query("SELECT id, name, address from customers", function (records) {
    res.write('<h1>Customers</h1>\n');
    res.write('<table>\n');
    for (var i = 0; i < records.length; i++) {
        res.write('<tr>\n');
        res.write('<td>' + records[i].id + '</td>\n');
        res.write('<td>' + records[i].name + '</td>\n');
        res.write('<td>' + records[i].address + '</td>\n');
        res.write('</tr>');
    }
    res.write('</table>\n');
    res.end();
  });
}); 
/*
 * Start web server
 */
app.listen(3000);

This is the output of http://localhost:3000/customers page:

What an app! 😉 😉

My links about Node.Js:

http://delicious.com/ajlopez/nodejs

My plan: implement the server in AjSharp; go for a complete CRUD pages for a table using Node.js and Sqlite3; switch to the other node-sqlite3 binding; generate code for all.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Create a free website or blog at WordPress.com.