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

October 9, 2010

Running AjGenesis using Mono and Ubuntu

Filed under: .NET, AjGenesis, Code Generation, Mono, Ubuntu — ajlopez @ 8:52 am

In these days, I am involved in the developing of a new version (complete rewriting) of a Java application, with a web interface. It is a team effort, and the majority of the members are Ubuntu fans: there are more proficient working in Linux boxes. The project has code generation using (make an educated guess.. ;-) AjGenesis, my open source code generation project. Then, the team want to run AjGenesis out of Windows.

AjGenesis is written in VB.NET, and compiled for .NET 2.0. I was afraid that I should make some change to run it under Ubuntu using Mono project. But I was wrong. Ubuntu has  Mono pre-installed. But AjGenesis use VB.NET runtime. Well, Mono has a VB.NET compiler. I launched the terminal in my Ubuntu ( in Virtual Box over Windows 2008), and executed:

sudo apt-get install mono-vbnc

(I guess this step is needed, but I didn’t try to run AjGenesis with only Mono installed).

Then, I downloaded the AjGenesis compiled binaries (from source trunk). And some examples, like AppExampleStep06.zip (described in Building an Application using AjGenesis (Part 6)).

You can run AjGenesis.Console.exe using

mono AjGenesis.Console.exe

But I marked it as executable:

chmod +xr- AjGenesis.Console.exe

so you can run it directly:

The Build folder was created! Nice!

After this first experiment, I got the source code of the project using subversion, and run the code generation script. The only gotchats were some tasks, written in AjBasic, that were creating directories in lower case and using them with one upper case letter. I fixed the bug, and now, the code generation is running. Team members are able to develop in Windows and Ubuntu, using Java, Tomcat, Maven, and AjGenesis.

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

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

Follow

Get every new post delivered to your Inbox.

Join 57 other followers