Check out a free preview of the full The Hard Parts of Servers & Node.js course

The "Intro to Require in Node" Lesson is part of the full, The Hard Parts of Servers & Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Will explains the value of using the built-in require method to access certain features in Node.

Preview
Close

Transcript from the "Intro to Require in Node" Lesson

[00:00:00]
>> Will Sentance: Next thing, getting access to Node's built in features in C++. I said to you that http.createServer was a label built in to JavaScript to access features in the background. It's not quite. We need one line to turn it on. It's a very simple line. We may have time later on to go into detail about how this line works, really fascinating.

[00:00:22]
If you've seen the closure Hard Parts, it uses that in a really cool way. We may not have time, if not, we'll do it in Hard Parts Node Part Two.
>> Will Sentance: Invite me back, [COUGH], anyway, so we have to tell Node we wanna have access to each of its C++ features independently.

[00:00:44]
That's by design. We do that so that we don't bring in access points to all of these features all at once. Because what if I want to go to a mail server, M-A-I-L? A mail server that wants to receive or send out emails? That would not need HTTP as its core protocol.

[00:01:05]
So why pull in the HTTP access point, labels? I wouldn't bother. So it's a way of keeping only labels available in JavaScript for Node C++ features that I actually need. And I just run it like this. runRequire, now that is a built in function, although, again, if we do get to modularity, we'll see it's not really built-in.

[00:01:26]
It's actually available in this really cool way that's related to how the common JS pattern works, which is where require comes from. But for now, let's just think it was built-in, when you run require, anywhere in an Node file you have require available, whatever you pass to it, it's gonna look at that and go, you want access to the HTTP features.

[00:01:44]
Let me give you all of the, and an insert, that's what it actually does. It's gonna say, require's gonna put, in your memory, label HTTP that will be for an object full of functions, which include createServer, exactly, createServer,
>> Will Sentance: And that function is the one that actually sets up the background.

[00:02:09]
But think about it no different than setTimeout. We'll see later on that setTimeout is not a real JavaScript function, it's a label for a Node C++ built timer. There's no timers in JavaScript, but setTimeout, we don't have to tell, Node, hey, can you put setTimeout in the mix?

[00:02:25]
It's there already. HTTP and all the other ones, to kick off Node C++ features, we have to tell it by using require, bringing those in.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now