JavaScript, Programming

Posts tagged nodejs

53 notes &

Hosting NodeJS Apps on CentOS 5

I recently enjoyed a great article by Alex Young from DailyJS, called “Hosting Node Apps” (http://dailyjs.com/2010/03/15/hosting-nodejs-apps/).  However, my server runs on CentOS so setting it up was a little bit different.  Here’s what I did to setup, hopefully it helps you out as well.

Grab Core Dependencies

yum install sudo nginx unzip gcc-c++ screen git-core monit

Install NodeJS


git clone git://github.com/joyent/node.git

then Build

make install

Configure nginx Web Server

Edit the virtual host configuration file at:


Add the following to the end of the file:

upstream app_cluster_1 {

server {
        server_name nodetest.local nodetest;
        access_log /var/log/nginx/nodetest.log;

        location / {
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_set_header X-NginX-Proxy true;

          proxy_pass http://app_cluster_1/;
          proxy_redirect off;

Save and reload nginx:

service nginx reload

Create a Node App

Make a quick sample app at:


Putting in the following contents:

var sys = require("sys"),
   http = require("http");
http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World");
sys.puts("Server running at");

Set up Monit

Create a new configuration file for monit at:


Putting in the following contents:

check host hello_world with address
    start program = "/usr/local/bin/node /var/www/apps/hello_world/example.js"
    stop program  = "/usr/bin/pkill -f 'node /var/www/apps/hello_world/example.js'"
    if failed port 8000 protocol HTTP
        request /
        with timeout 10 seconds
        then restart

Then restart monit for the configuration to take place:

monit start all && service monit restart

All set!

Go ahead and point your browser to http://nodetest.local to see a “Hello World” page (if everything worked!).  For more details on the how and why and for additional resources, check out Alex’s article.  This is just intended to give a quick start for CentOS 5 users since some of the commands and packages are different.

Filed under nodejs centos

4 notes &

Adventures in NodeJS - CSV to SQL VALUES

Been recently getting into NodeJS, server side evented-IO JavaScript goodness.  Here is a little app I wrote today to convert CSV files into SQL VALUES statements to use when dumping one schema to another.  Its not complete but was able to help me in my use case:

var sys = require("sys"),
    fs = require("fs"),
    argv_length = process.argv.length,
    csv = process.argv[argv_length-1];

    if(err) throw err;

    var split_regex = /,$|("[^"]+(?:"),)|([^,]+(?:,))|,|[^,]+/g,
        records = data.split("\n");

    function csvRowToArray(row){
        var csvArray = [],
            matches = row.match(split_regex);
        if(matches != null){
        return csvArray;

    for(var i = 1, len = records.length; i < len; i++){
        var record = csvRowToArray(records[i]);
        sys.puts("(\""+record.join("\",\"") + "\"),");

To run it, first install Node (see link above) on your machine and from the command line type:

node file.js mycsv.csv

Where ‘file.js’ is whatever you call the snippet above and the first argument being the name of the .csv you want to have converted. It will output it to the terminal but you could easy write it to a file with:

node file.js mycsv.csv > output.txt


Filed under nodejs javascript