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

Why do we need to put || var http = require("http") ||

I don't quite understand why (or what this does) we need to declare this:

var http = require("http");

I THINK I understand why and what this does:

var profile = require("./profile.js");


This is my explanation of what the Var profile does:

Correct me if I'm wrong: The

var profile = require("./profile.js");

Grabs the profile Object from "../profile.js" and saves the profile API/JSON data into an object named profile. Once we have the profile variable generated from the ../profile.js page, we can manipulate it and add more "search features" to that in the app.js, and then send the profile variable from the "../app.js" page back to the "../profile.js" page and gather further data.

Asynchronous programming at its finest right?


However, I don't understand the "what or why" for the

var http = require("http");

Can someone please enlighten me? Thanks!

Snapshot of workspace: https://w.trhou.se/p9s4674pb7

2 Answers

From: https://nodejs.org/api/http.html#http_http

To use the HTTP server and client one must require('http').

My understanding is that requiring it gives you access to Node's http API, through the http variable you have created.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Jeff;

Take a look here and post back if you still have questions. Basically the require statement loads, or includes the referenced file or module into the host file and gets assigned to a variable name allowing the module's exported functions to be accessed.

Happy coding,
Ken

Thanks for the help!

For clarification: So by saying:

var http = require("http");

I am assigning the Variable http to the "http" method? And the 'http method' can be found here

Ken Alger
Ken Alger
Treehouse Teacher

Jeff;

Yes. Your could, or course, do something completely different and unorthodox like:

var blueDog = require("http");

But, I would argue, that while functional it will be highly confusing later on. Therefore you will find most of the var naming conventions will use the module name, like http, or an abbreviation like:

var exphbs = require("express-handlebars");

You will see similar conventions for file names as in your profile.js example above.

If we take a look at http and the documentation you will see all sorts of related methods that the http module provides. Things like ServerRequest, ServerResponse, ClientResponse, etc. In our code, since the module has exported those functions, we can with our http variable do something like http.ServerRequest(...). One should be able to look at that and know that what is inside that function is making a request from an http server. Right? See how my example of blueDog.ServerRequest(...) would quickly become confusing?

Hopefully that helps a bit.

Post back with other questions and happy coding,
Ken