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

6 Comments »

  1. [...] This post was mentioned on Twitter by ajlopez and luis petek, Jim Burger. Jim Burger said: RT @ajlopez: Daily post: Playing with Node.js, Ubuntu, Sqlite3, Node-Sqlite (English) http://bit.ly/fU4Snv [...]

    Pingback by Tweets that mention Playing with Node.js, Ubuntu, Sqlite3 and node-Sqlite « Angel “Java” Lopez on Blog -- Topsy.com — January 31, 2011 @ 11:25 am

  2. [...] Playing with Node.js, Ubuntu, Sqlite3 and node-Sqlite (tags: node.js javascript sqlite ubuntu) Categories: Links LikeBe the first to like this post. Comments (0) Trackbacks (0) Leave a comment Trackback [...]

    Pingback by links for 2011-01-31 « Bloggitation — February 1, 2011 @ 6:03 am

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

    Pingback by Playing With NodeJs (1) Running on Windows (and Azure) « Angel “Java” Lopez on Blog — September 1, 2011 @ 11:18 am

  4. Could you please give me more details about the “build” problem? I use “sudo apt-get install libsqlite3-dev” but I’m still having the same error “Build failed: -> task failed (err #1):
    {task: cxx sqlite3_bindings.cc -> sqlite3_bindings_1.o}”

    Comment by mariosk89 — November 20, 2012 @ 3:11 pm

  5. This paragraph will help the internet people for building
    up new weblog or even a weblog from start to end.

    Comment by Singapore Graphic Design — July 22, 2013 @ 8:34 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 57 other followers

%d bloggers like this: