Tag Archives: node.js

How to update multiple documents in mongodb native nodejs driver?

Salam (means hello) :)

I'm running mongodb 2.4.8 and Mongo DB Native NodeJS Driver. when I use the following function, only first document that matched query updates. how can I update all matching documents?

function update(coll, query, update, callback){
    var options = options || {};
    MongoClient.connect('mongodb://127.0.0.1:27017/dbName', function(error, db) {
        if(error){
            return console.dir(error);
        }
        db.collection(coll).update(query, update, {w:1}, function(error, result) {
            callback(error, result);
        });
    });
}

I installed my mongodb driver via npm install mongodb command, which installs version 1.3.23, does this driver version support multi update? if not, how can I install a newer version of driver supports multi update?

Serving zipped files in nodejs, how to cache instead of recompress?

Salam (means Hello) :)

I'm developing a static file server that must support data-encoding: gzip, the example in zlib documentation is working fine for me, but I need to save zipped files to disk, to avoid re-compressing for future requests. here is my code, which doesn't work:

http.createServer(function(request, response) {
    var url = my_normalizer(request.url);
    var acceptEncoding = request.headers['accept-encoding'] || '';

    if(acceptEncoding.match(/\bgzip\b/)){
        try{
            var zipped = fs.createReadStream(url +'.gz');
        }catch(e){
            var inp = fs.createReadStream(url);
            var out = fs.createWriteStream(url+'.gz');
            inp.pipe(zlib.createGzip()).pipe(out);
            var zipped = fs.createReadStream(url +'.gz');
        }
        response.writeHead(200, { 'content-encoding': 'gzip' });
        zipped.pipe(response);
    }
}).listen(80);

and this is the result in firefox:

Content Encoding Error

The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.

for what it's worth, a .gz version of the requested file is correctly generated and saved to disk.

How to submit a POST request to Apache Syncope API in node.js?

Salam (means Hello) :)

In my node.js application, I am trying to add a user to Apache Syncope via its RESTful API. Apache Syncope is installed on a remote VPS and this article demonstrate this process in a PHP environment. here is what I did in my node.js application:

http.createServer(onRequest).listen(80);

function onRequest (request, response) {
    var options = {
        hostname: MY_VPS_IP,
        port: 8080,
        path: '/syncope/rest/user/create.json',
        auth: 'admin:password',
        method: 'POST'
    };
    var data = {an: 'object', of: 'new', user: 'data' };

    var req=http.request(options, function(res) {
        var body;
        res.on('data', function (chunk) {
            body += chunk;
        });
        res.on('end', function () {
            response.writeHead(200, { 'Content-Type': 'text/html' });
            response.end(body, 'utf-8');
        });
    });
    req.end(JSON.stringify(data));
}

The response of Apache Syncope is a 415 error (css styles omitted):

undefined
<html>
<head><title>Apache Tomcat/7.0.42 - Error report</title>
</head>
<body><h1>HTTP Status 415 - </h1>
<HR size="1" noshade="noshade">
<p><b>type</b> Status report</p>

<p><b>message</b> <u></u></p>

<p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the
    requested resource for the requested method.</u></p>
<HR size="1" noshade="noshade">
<h3>Apache Tomcat/7.0.42</h3></body>
</html>

How to submit a POST request to Apache Syncope API in node.js?

Salam (means Hello) :)

In my node.js application, I am trying to add a user to Apache Syncope via its RESTful API. Apache Syncope is installed on a remote VPS and this article demonstrate this process in a PHP environment. here is what I did in my node.js application:

http.createServer(onRequest).listen(80);

function onRequest (request, response) {
    var options = {
        hostname: MY_VPS_IP,
        port: 8080,
        path: '/syncope/rest/user/create.json',
        auth: 'admin:password',
        method: 'POST'
    };
    var data = {an: 'object', of: 'new', user: 'data' };

    var req=http.request(options, function(res) {
        var body;
        res.on('data', function (chunk) {
            body += chunk;
        });
        res.on('end', function () {
            response.writeHead(200, { 'Content-Type': 'text/html' });
            response.end(body, 'utf-8');
        });
    });
    req.end(JSON.stringify(data));
}

The response of Apache Syncope is a 415 error (css styles omitted):

undefined
<html>
<head><title>Apache Tomcat/7.0.42 - Error report</title>
</head>
<body><h1>HTTP Status 415 - </h1>
<HR size="1" noshade="noshade">
<p><b>type</b> Status report</p>

<p><b>message</b> <u></u></p>

<p><b>description</b> <u>The server refused this request because the request entity is in a format not supported by the
    requested resource for the requested method.</u></p>
<HR size="1" noshade="noshade">
<h3>Apache Tomcat/7.0.42</h3></body>
</html>

How to use a different version of python during NPM install?

I have terminal access to a VPS running centos 5.9 and default python 2.4.3 installed. I also installed python 2.7.3 via these commands: (I used make altinstall instead of make install)

wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -xf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make
make altinstall

then I installed node.js from source via these commands:

python2.7 ./configure
make
make install

The problem is, when I use npm install and try to install a node.js package which requires python > 2.4.3 I get this error:

gyp ERR! configure error
gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at failPythonVersion (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:125:14)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:114:9

how should I "pass the --python switch to point to Python >= v2.5.0"?

How to use a different version of python duing NPM install?

Salam (means Hello) :)

I have terminal access to a VPS running centos 5.9 and default python 2.4.3 installed. I also installed python 2.7.3 via these commands: (I used make altinstall instead of make install)

wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -xf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make
make altinstall

then I installed node.js from source via these commands:

python2.7 ./configure
make
make install

The problem is, when I use npm install and try to install a node.js package which requires python > 2.4.3 I get this error:

gyp ERR! configure error
gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at failPythonVersion (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:125:14)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:114:9

how should I "pass the --python switch to point to Python >= v2.5.0"?

How to use a different version of python duing NPM install?

Salam (means Hello) :)

I have terminal access to a VPS running centos 5.9 and default python 2.4.3 installed. I also installed python 2.7.3 via these commands: (I used make altinstall instead of make install)

wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -xf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make
make altinstall

then I installed node.js from source via these commands:

python2.7 ./configure
make
make install

The problem is, when I use npm install and try to install a node.js package which requires python > 2.4.3 I get this error:

gyp ERR! configure error
gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at failPythonVersion (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:125:14)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:114:9

how should I "pass the --python switch to point to Python >= v2.5.0"?

How to use a different version of python duing NPM install?

Salam (means Hello) :)

I have terminal access to a VPS running centos 5.9 and default python 2.4.3 installed. I also installed python 2.7.3 via these commands: (I used make altinstall instead of make install)

wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -xf Python-2.7.3.tgz
cd Python-2.7.3
./configure
make
make altinstall

then I installed node.js from source via these commands:

python2.7 ./configure
make
make install

The problem is, when I use npm install and try to install a node.js package which requires python > 2.4.3 I get this error:

gyp ERR! configure error
gyp ERR! stack Error: Python executable "python" is v2.4.3, which is not supported by gyp.
gyp ERR! stack You can pass the --python switch to point to Python >= v2.5.0 & < 3.0.0.
gyp ERR! stack     at failPythonVersion (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:125:14)
gyp ERR! stack     at /usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:114:9

how should I "pass the --python switch to point to Python >= v2.5.0"?

NPM doesn’t install any modules: network socket hangs up

Salam (means Hello) :)

I have the latest version of node.js installed on ubuntu 12.04, I'm not behind any proxies, and my network settings are correctly configured, and were intact since last time when NPM worked fine. But now NPM hangs up installation of any modules with following error:

nasser@nasser-desktop:~/projects/server v3$ npm install simple-proxy
npm WARN package.json docco@0.6.2 No repository field.
npm http GET https://registry.npmjs.org/simple-proxy
npm http GET https://registry.npmjs.org/simple-proxy
npm http GET https://registry.npmjs.org/simple-proxy
npm ERR! network socket hang up
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
npm ERR! System Linux 3.5.0-17-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "simple-proxy"
npm ERR! cwd /home/nasser/projects/serverV3
npm ERR! node -v v0.10.18
npm ERR! npm -v 1.3.8
npm ERR! code ECONNRESET
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/nasser/projects/serverV3/npm-debug.log
npm ERR! not ok code 0

NPM doesn’t install any modules: network socket hangs up

Salam :)

I have the latest version of node.js installed on ubuntu 12.04, I'm not behind any proxies, and my network settings are correctly configured, and were intact since last time when NPM worked fine. But now NPM hangs up installation of any modules with following error:

nasser@nasser-desktop:~/projects/server v3$ npm install simple-proxy
npm WARN package.json docco@0.6.2 No repository field.
npm http GET https://registry.npmjs.org/simple-proxy
npm http GET https://registry.npmjs.org/simple-proxy
npm http GET https://registry.npmjs.org/simple-proxy
npm ERR! network socket hang up
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network 
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'
npm ERR! System Linux 3.5.0-17-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "simple-proxy"
npm ERR! cwd /home/nasser/projects/serverV3
npm ERR! node -v v0.10.18
npm ERR! npm -v 1.3.8
npm ERR! code ECONNRESET
npm ERR! 
npm ERR! Additional logging details can be found in:
npm ERR!     /home/nasser/projects/serverV3/npm-debug.log
npm ERR! not ok code 0

How to create a simple http proxy in node.js?

I'm trying to create a proxy server to pass HTTP GET requests from a client to a third party website (say google). My proxy just needs to mirror incoming requests to their corresponding path on the target site, so if my client's requested url is:

127.0.0.1/images/srpr/logo11w.png

the following resource should be served:

http://www.google.com/images/srpr/logo11w.png

here is what I came up with:

http.createServer(onRequest).listen(80);

function onRequest (client_req, client_res) {
    client_req.addListener("end", function() {
        var options = {
            hostname: 'www.google.com',
            port: 80,
            path: client_req.url,
            method: 'GET'
        };
        var req=http.request(options, function(res) {
            var body;
            res.on('data', function (chunk) {
                body += chunk;
            });
            res.on('end', function () {
                 client_res.writeHead(res.statusCode, res.headers);
                 client_res.end(body);
            });
        });
        req.end();
    });
}

it works well with html pages, but for other types of files, it just returns a blank page or some error message from target site (which varies in different sites).

How to create a simple http proxy in node.js?

Salam (means hello) :)

I'm trying to create a proxy server to pass HTTP GET requests from a client to a third party website (say google). My proxy just needs to mirror incoming requests to their corresponding path on the target site, so if my client's requested url is:

127.0.0.1/images/srpr/logo11w.png

the following resource should be served:

http://www.google.com/images/srpr/logo11w.png

here is what I came up with:

http.createServer(onRequest).listen(80);

function onRequest (client_req, client_res) {
    client_req.addListener("end", function() {
        var options = {
            hostname: 'www.google.com',
            port: 80,
            path: client_req.url,
            method: 'GET'
        };
        var req=http.request(options, function(res) {
            var body;
            res.on('data', function (chunk) {
                body += chunk;
            });
            res.on('end', function () {
                 client_res.writeHead(200, { 'Content-Type': mime_of(client_req.url)});
                 client_res.end(body);
            });
        });
        req.end();
    });
}

it works well with html pages, but for other types of files, it just returns a blank page or some error message from target site (which varies in different sites).

How to open a port below 1024 in Cent OS for socket.io?

I'm trying to open port 843 in my Cent OS 5.2, I added following line to etc\sysconfig\iptables :

-A INPUT -p tcp --dport 843 -j ACCEPT

and then updated my iptables service. I need to listen to that port in my node.js application which is running by root user via sudo node index.js command, but I still get forbidden port error when I try establish a connection through that port.

this is what I get by running sudo iptables -L -v :

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination                     
 2811  238K ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:http
  112  6224 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:hbci
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:843
   41  2124 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp multiport dports 5901:5903,6001:6003
13093   13M ACCEPT     all  --  any    any     anywhere             anywhere                                state RELATED,ESTABLISHED
   26  3584 ACCEPT     icmp --  any    any     anywhere             anywhere                        
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere                        
  109  6404 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp dpt:ssh
 888K   80M REJECT     all  --  any    any     anywhere             anywhere 

and this is outpou of sudo netstat -ptl | grep node:

tcp        0      0 *:843       *:*    LISTEN      12927/node
tcp        0      0 *:http      *:*    LISTEN      12927/node
tcp        0      0 *:10843     *:*    LISTEN      12927/node

and I try to listen to that port in socket.io with this code:

var io          = require('socket.io').listen(
                    843,
                    {   log: false,
                        flashPolicyServer: true,
                        transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
                    }
                );
io.sockets.on('connection', function(socket){
    // my event listeners
});

How to open a port below 1024 in Cent OS for socket.io?

I'm trying to open port 843 in my Cent OS 5.2, I added following line to etcsysconfigiptables :

-A INPUT -p tcp --dport 843 -j ACCEPT

and then updated my iptables service. I need to listen to that port in my node.js application which is running by root user via sudo node index.js command, but I still get forbidden port error when I try establish a connection through that port.

this is what I get by running sudo iptables -L -v :

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination                     
 2811  238K ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:http
  112  6224 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:hbci
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:843
   41  2124 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp multiport dports 5901:5903,6001:6003
13093   13M ACCEPT     all  --  any    any     anywhere             anywhere                                state RELATED,ESTABLISHED
   26  3584 ACCEPT     icmp --  any    any     anywhere             anywhere                        
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere                        
  109  6404 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp dpt:ssh
 888K   80M REJECT     all  --  any    any     anywhere             anywhere 

and this is outpou of sudo netstat -ptl | grep node:

tcp        0      0 *:843       *:*    LISTEN      12927/node
tcp        0      0 *:http      *:*    LISTEN      12927/node
tcp        0      0 *:10843     *:*    LISTEN      12927/node

and I try to listen to that port in socket.io with this code:

var io          = require('socket.io').listen(
                    843,
                    {   log: false,
                        flashPolicyServer: true,
                        transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
                    }
                );
io.sockets.on('connection', function(socket){
    // my event listeners
});

How to open a port below 1024 in Cent OS for socket.io?

Salam (means Hello) :)

I'm trying to open port 843 in my Cent OS 5.2, I added following line to etc\sysconfig\iptables :

-A INPUT -p tcp --dport 843 -j ACCEPT

and then updated my iptables service. I need to listen to that port in my node.js application which is running by root user via sudo node index.js command, but I still get forbidden port error when I try establish a connection through that port.

this is what I get by running sudo iptables -L -v :

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination                     
 2811  238K ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:http
  112  6224 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:hbci
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere                                tcp dpt:843
   41  2124 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp multiport dports 5901:5903,6001:6003
13093   13M ACCEPT     all  --  any    any     anywhere             anywhere                                state RELATED,ESTABLISHED
   26  3584 ACCEPT     icmp --  any    any     anywhere             anywhere                        
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere                        
  109  6404 ACCEPT     tcp  --  any    any     anywhere             anywhere                                state NEW tcp dpt:ssh
 888K   80M REJECT     all  --  any    any     anywhere             anywhere 

and this is outpou of sudo netstat -ptl | grep node:

tcp        0      0 *:843       *:*    LISTEN      12927/node
tcp        0      0 *:http      *:*    LISTEN      12927/node
tcp        0      0 *:10843     *:*    LISTEN      12927/node

and I try to listen to that port in socket.io with this code:

var io          = require('socket.io').listen(
                    843,
                    {   log: false,
                        flashPolicyServer: true,
                        transports: ['websocket', 'flashsocket', 'htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling']
                    }
                );
io.sockets.on('connection', function(socket){
    // my event listeners
});