Skip to content

Instantly share code, notes, and snippets.

@ecto
Last active December 27, 2015 12:19
Show Gist options
  • Save ecto/7324775 to your computer and use it in GitHub Desktop.
Save ecto/7324775 to your computer and use it in GitHub Desktop.

Revisions

  1. ecto revised this gist Nov 5, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    var net = require('net');
    var url = require('url');

    // edit this (password)
    // edit this
    var conString = 'tcp://crypton_test_user:crypton_test_user_password@localhost:5432/crypton_test'; // default

    var config = parse(conString);
  2. ecto renamed this gist Nov 5, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. ecto created this gist Nov 5, 2013.
    57 changes: 57 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    ~ → cat testConnection.js

    var net = require('net');
    var url = require('url');

    // edit this (password)
    var conString = 'tcp://crypton_test_user:crypton_test_user_password@localhost:5432/crypton_test'; // default

    var config = parse(conString);
    console.log(config);
    var stream = new net.Stream();

    stream.on('connect', function () {
    console.log('connected', arguments);
    });
    stream.on('data', function () {
    console.log('data', arguments);
    });
    stream.on('error', function () {
    console.log('error', arguments);
    });
    stream.on('end', function () {
    console.log('end', arguments);
    });

    stream.connect(config.port, config.host);

    // Below is code from the node-postgres module

    function parse (str) {
    //unix socket
    if(str.charAt(0) === '/') {
    return { host: str };
    }
    // url parse expects spaces encoded as %20
    str = encodeURI(str);
    var result = url.parse(str);
    var config = {};
    config.host = result.hostname;
    config.database = result.pathname ? result.pathname.slice(1) : null;
    var auth = (result.auth || ':').split(':');
    config.user = auth[0];
    config.password = auth[1];
    config.port = result.port;
    return config;
    };


    ~ → node testConnection.js

    { host: 'localhost',
    database: 'crypton_test',
    user: 'crypton_test_user',
    password: 'crypton_test_user_password',
    port: '5432' }
    connected {}
    ^C%