How to use DocumentDB behind a Proxy in NodeJS - node.js

In NodeJS I`m trying to connect to a Cosmos DB using the Documentdb library, as the getting starter of the azure documentation says in the TODO List Example. Tutorial Here
If I use an Internet that is not behind a proxy it works.
This is the connection code:
var DocumentDBClient = require('documentdb').DocumentClient;
var docDbClient = new DocumentDBClient(config.host, {
masterKey: config.authKey
});
But when I'm behind a proxy, the connection never occur's. I`m getting an "Error: connect ETIMEDOUT"
In other node JS apps, if I want to make a request of a webservice, I just configure the proxy for the request. For example with request:
request = require('request').defaults({
proxy:'http://USERNAME:PASSWORD#proxy.url.com:8080',
});
Is there a way to configure the proxy in DocumentDB to connect to the DB in Azure (NodeJS)?

I've not personally tried it but I was going through the source code of the SDK and found out that ConnectionPolicy has a parameter called ProxyUrl. Can you try something like the following:
var connectionPolicy = new DocumentBase.ConnectionPolicy();
connectionPolicy.ProxyUrl = 'http://USERNAME:PASSWORD#proxy.url.com:8080';
var docDbClient = new DocumentDBClient(config.host, {
masterKey: config.authKey
}, connectionPolicy);

Related

Connect AWS redis to node using node-redis

I am using node-redis and having a hard time connecting to external redis instance. I tried with redis-cli and it worked. However with node I am not able to figure out how to properly give the url and port.
With Redis-cli-
redis-cli -h mydomain.something.something.cache.amazonaws.com -p 6379
However with nodejs
Below didn't work
var client = redis.createClient('redis://mydomain.something.something.cache.amazonaws.com:6379'),
neither
var client = redis.createClient({host:'redis://mydomain.something.something.cache.amazonaws.com', port: 6379});
How do I configure it. Please help.
Following should work with node.js -
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
Also, make sure that your security group on AWS allows you to access the database.
var client = require('redis').createClient(6379, 'elastichache endpoint string', {
no_ready_check: true
});
With the above code, it was always trying to connect with localhost,
Below code worked for me.
var client = require('redis').createClient(
{
url: `redis://${elasticCacheConnectionString}`,
}
);
Please note, i have appended redis:// as communication protocol before actual connection string.
FYI: I am using redis#4.1.0 version.

Is it possible to browserify the "tedious" module so that the nodejs program can be run in the browser?

I'm a beginner to Node.js and I'm currently building a Node.js program that accesses and queries a Microsoft Azure SQL database with the "tedious" module (see code below) and puts the data onto a html webpage. I want to run this code in a browser so I used browserify to bundle the modules together. However, when this code is run in Google Chrome, the following error is returned: require is not defined. Is there a fix? Is it even possible to use the tedious module in Chrome? If it isn't possible, do I need to use an intermediate server between the Node.js application and the webpage?
var Connection = require('tedious').Connection;
var config = {
userName: 'hackmatch',
password: 'hackvalley123!',
server: 'hackmatch.database.windows.net',
options: {encrypt: true, database: 'AdventureWorks'}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
Thanks in advance for your help! :)
No. This module can only be used in Node.
tedious depends on the node.js net module to make a connection to the database server. This module has no equivalent on the browser, as web pages cannot make arbitrary network connections.
Even if it were possible to use this module in the browser, it'd be a terrible idea. You'd be allowing anyone on your web site to connect directly to your SQL server and run SQL queries. This can only end badly.

Read data from a url behind a proxy

WEB Server I want to connect to
I have a web service running in a private network. This server is a web service which I can see working in the browser if I set the socks proxy in the browser.
My Service
I need node.js server on my machine to use the socks proxy to connect and call the web server
My UseCase
I need to do post requests for xml data as well as do some get requests.
My Problem
My app is not able to connect to the server hidden behind the socks proxy.
I do not want to set the global proxy for node or anything, only for one part of the app.
Updated : Working Solution
While the answer directs in the correct direction, I will include the final working solution here for reference as it needed a few modifications to the examples on github
var shttp = require('socks5-http-client');
var options = {} ;
options.host = 'ip.of.web.service';
options.port = 1919; //port of webservice
options.path = '/control/getjson'; //path on webservice to get
options.socksPort = 8778; //socks proxy port
options.socksHost = 'ip.of.socks.proxy';
var req = shttp.get(options, function (res) {
res.setEncoding('utf8');
res.on('readable', function () {
callback(res); //send response to my function for further processing.
});
});
Using socks proxy is not natively supported in the built in http client object.
Following 2 libraries makes it easy to connect to http endpoints through a socks proxy. Give it a try
Use socks5-http-client for connecting to http endpoints
Use socks5-https-client for connecting to https endpoints

access base http server of sails.js

Hi is there a way to access the base http server context of sails? I want to use binaryJS in my app and In the gettig started guide they are talking about creating the server at your own, it you have an existing express app with the following line:
var server = http.createServer(app).listen(9000);
than I could use the following binaryJS command:
// Create a BinaryServer attached to our existing server
var binaryserver = new BinaryServer({server: server, path: '/binary-endpoint'});
thank you
In Sails v0.10.x, you can access the underlying Express server with:
sails.hooks.http.server
in v0.9.x, it's
sails.express.server

First request to node.js app on Windows Azure with MongoDB yields 400 Bad Request

I'm using Windows Azure to deploy a node.js application that I've written that exposes a fairly simple REST CRUD api to clients. Its hosted in a Windows Azure Website and uses MongoDB through the Windows Azure store with mongoose. The requests I'm making to the service are JSON and the responses are JSON (not sure that matters but others have talked about 400 responses to requests with a Content-Type of application/json)
On the first access in a long while, the application returns 400 Bad Request without fail. As long as I keep the application "warm" by hitting it frequently (at least once a minute or so) - I never seem to get this again.
It doesn't matter on the hosting scaling setting - I get the same thing on the free tier as in reserved mode.
Anyone else seen this?
In order to guarantee access to any connection in node.js, you have to put all code that requires a connection inside of the callback. The way that mongoose exposes this connection is through an event. When the event 'open' is called by the mongoose connection, then you have access to a database connection.
I.E.
mongoose.connect('details');
mongoose.on('open', function () {
var connection = mongoose.connection;
// Do things with your connection here
doThings(connection);
});
function doThings(connection) {
app.get(...);
}
It would be helpful if you had a code snippet, but my guess is that your connection to mongo is happening asynchronously and your site is serving the request before the connection is actually open.
I tested this scenario and couldn't reproduce the issue. You can see my code here: https://github.com/ntotten/azure-mongo-sample
Basically, I am using mongoose to connect and the connection is happening right away when the app is loaded. You can see the code below.
app.js:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var TaskList = require('./routes/tasklist');
var taskList = new TaskList(process.env.CUSTOMCONNSTR_MONGOLAB_URI);
...
tasklist.js:
var mongoose = require('mongoose')
, task = require('../models/task.js');
module.exports = TaskList;
function TaskList(connection) {
mongoose.connect(connection);
}
...

Resources