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

Using http.get with the https protocol instead of http?

How would I connect to an api with http.get that uses the https protocol instead of http? When I tried to do this the normal way, I got a 301 status code.

May I ask which API are you trying to use?

I'm using the forecast.io API with Nodejs.

Not sure if you already tried this, but did you try any of those unofficial Node.JS wrapper libraries listed in the API docs?

Forecast API Docs

I'm doing this to practice. I looked at them for hints, but I don't really understand them.

1 Answer

Andrew Barrette
PLUS
Andrew Barrette
Courses Plus Student 2,219 Points

I ran into this same issue. As you might expect, the http Node module does not handle https requests. If you look at the Node.js docs, you'll see a separate module for https.

In practice, what this means is that instead of:

var http = require('http');

you'll simply want to require the https module. Like so:

var https = require('https');

The basic reason for this (as far as I understand) is that https requests are handled in a different (more secure) way, so Node simply uses separate modules for each.