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.
~ → cat testConnection.js
var net = require('net');
var url = require('url');
// edit this
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%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment