Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Vincent DUBROEUCQ
Vincent DUBROEUCQ
61,690 Points

Problem switching to HTTPS requests in Node.js

Hi, I have a problem with Node.js HTTP requests as explained in Node.js Basics. HTTP requests to the Treehouse JSON profiles do not work since they switched to HTTPS. However, https doesn't work either. I correctly required HTTPS and even tried the HTTPS.request method, because I can specify the protocol in the options.

Here's the code

var https = require('https'); var username = 'vincentdubroeucq'; var url = 'https://www.teamtreehouse.com/' + username + '.json' var options = { protocol: 'https:', hostname: 'www.teamtreehouse.com', path: '/' + username + '.json' };

var request = https.request(options, (response) => { console.log('statusCode: ', response.statusCode); console.log('headers: ', response.headers); response.on('data', (data) => { process.stdout.write(data); }); });

However, I still get an http location when I log the header in the console. location: 'http://teamtreehouse.com/vincentdubroeucq.json'

Any ideas?

2 Answers

Jeff Wilton
Jeff Wilton
16,646 Points

It appears to be a problem with www. in the hostname causing the request to use http instead of https. I messed around with a bunch of different options, but finally I realized that removing the www. from your hostname option did the trick.

var https = require('https'); 
var username = 'vincentdubroeucq'; 
// var url = 'https://www.teamtreehouse.com/' + username + '.json';
var options = { 
  // protocol: 'https:', --this isn't necessary
  hostname: 'teamtreehouse.com', 
  path: '/' + username + '.json',
};
callback = function(response) { 
  var str = '';
//  console.log('statusCode: ', response.statusCode); 
//  console.log('headers: ', response.headers); 

  response.on('data', function(data){ 
    str += data;
  }); 

  response.on('end', function() {
    console.log(str);
  });
}

var request = https.request(options, callback);
request.end()
Vincent DUBROEUCQ
Vincent DUBROEUCQ
61,690 Points

Yeah! That works! You're a genius ! Thanks a lot ! Now the question is why does it work without the www. The 'static' url and the dynamically generated one are the same. That's weird. Still, thanks a lot for your help !

Jeff Wilton
Jeff Wilton
16,646 Points

I'm guessing the Teamtreehouse servers are redirecting their www traffic to a specific http port, but I'm not a DevOps guy so I can't speak to the dark wizardry they practice. Glad I could help you though - it was fun debugging the issue. :)

Vincent DUBROEUCQ
Vincent DUBROEUCQ
61,690 Points

I found something weird. When I type in the full url in the https.get method, everything works fine. However, if I use

var username = 'vincentdubroeucq';
var url = 'https://www.teamtreehouse.com/' + username + '.json'

var request = https.get(url, function(response) { ... }

It doesn't work.