Using Node.JS behind a proxy

I have my Raspberry Pi setup at work and it is stuck behind a proxy. Here are some helpful tips I have found from across the web:

 

Requests from Node

var http = require("http");

var options = {
  host: "proxy",
  port: 8080,
  path: "http://www.google.com",
  headers: {
    Host: "www.google.com"
  }
};
http.get(options, function(res) {
  console.log(res);
  res.pipe(process.stdout);
});

For HTTPS, Try this:


var http = require('http');

http.get ({
    host: '127.0.0.1',
    port: 8888,
    path: 'https://www.google.com/accounts/OAuthGetRequestToken'
}, function (response) {
    console.log (response);
});


In order to use NPM, try something like this:

  1. I run this command: npm config set strict-ssl false
  2. Then set npm to run with http, instead of https: npm config set registry "http://registry.npmjs.org/"
  3. Then I install packages using this syntax: npm --proxy http://username:password@cacheaddress.com.br:80 install packagename

Skip the username:password part if proxy doesn’t require you to authenticate

or this:

npm config set proxy http://proxy_host:port

or this:

npm --proxy http://proxy-server:8080/ install {package-name}

 

 

2 thoughts on “Using Node.JS behind a proxy”

  1. Wow this works!! Used behind a corporate firewall to install bower using NodeJS and it worked!

    Thanks Luke! :D

Leave a Reply

Your email address will not be published. Required fields are marked *