This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Audience Q&A continued" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

Scott continues his audience Q&A with a demonstration on how to use ES2015 features with Node.

Get Unlimited Access Now

Transcript from the "Audience Q&A continued" Lesson

>> [MUSIC]

>> Speaker 1: Any other questions?
>> Speaker 2: How can we use ES 2015 with node?
>> Speaker 1: That is an amazing question. So if you want partial support and if you run a node 12, you could just at the toggle file use strict and you'll get partial support as in you can do const and you can do let, and var, well, var is 2005.

[00:00:27] Yeah, so you can do that. If you want full support, then you need to transpile. So then you got to pick your flavor of transpilers. Probably only use Babble for a transpiler and then for tooling, Grunt, Gulp, Webpack, whatever tooling you want. But yeah, you pretty much just have to transpile it.

[00:00:43] And then the sweet thing about it is because node uses common js, as far as if you wanted to use the 2015 import syntax, then you're pretty much money on there because import's gonna compile down to common js which is great. In the browser, as you saw yesterday we had to use something like Webpack or JSPM to mock common js in the browser or system js, whereas Node.js does use common js so we don't have to figure that hard part out.

[00:01:09] But yeah, you have to use a transpiler and then a build tool to use that transpiler, or just use the transpiler from the command line, straight up from the command line. I'm just gonna run Babel. You can install Babel as a command line tool and run it there if you want to.

>> Speaker 2: Once you get it set up, is it pretty painless?
>> Speaker 1: Yeah, once you get it set up it's pretty painless. Depending on what you set it up with, I would say the easiest one in my opinion to set it up, Webpack is the easiest but then Webpack bundles it all and you don't need a bundled server file, but you could.

[00:01:41] Gulp is probably the next easiest. I wouldn't recommend JSPM with node. JSPM is more for client side, although you can use it with node and I have used it with node. It's really complicated because JSPM, like I said, it resolves URLs not relative to the file that you're in, but relative to where you tell system js is the entry point.

[00:02:01] So this wouldn't be /lions, it'd be like if our entry point was Node API, it'd be server/lions. So that doesn't compile down to common js very well. So you get some really wonky stuff with that and if you're not really comfortable with JavaScript, then you probably wanna stay away from that.

>> Speaker 1: Cool, any other questions?
>> Speaker 1: Nope, any other questions online, Mark?
>> Speaker 2: I think we're caught, let me.
>> Speaker 2: Is there a specific version of Node to have the ability to use let and const?
>> Speaker 1: Yeah, so if you're using, let me see what version of Node I'm using.

>> Speaker 1: I'm using 12.1 and I'm pretty sure I can do that, let me double-check. I'm just gonna run node. I'm going to use strict, and then I'll run and say const me = 'me'. Yeah, that works. So I'm using 12.1 and it works fine. let you = you.

[00:03:19] Okay, so it doesn't like let, but it likes const. So again node is using V.8. So whatever V.8 is bringing in, as far as ES 2015, that's what node has. And then it's also not on the same version as V.8, it's a little bit behind. So it's just gonna take a while.

[00:03:36] So if you want full support, definitely use a transpiler, which will give you everything. Cool. Anything else?
>> Speaker 2: Is IOJS a drop-in replacement for node? Are we doing anything in this course that isn't supported by IOJS?
>> Speaker 1: It's totally a drop-in replacement. Everything we're doing in this course will work exactly with IOJS.

[00:04:03] You just switch it right now and it'll work, if you wanna use IOJS right now it'll work the same way. Nothing will change. So yeah, you can totally do it. We're not doing anything you wont be able to do there.
>> Speaker 2: Can you explain the module about exports?

>> Speaker 1: Yeah, so.
>> Speaker 1: Let's make a new file. It'll be easier if I just show you in a brand new file.
>> Speaker 1: So let's say I have three variables, var mod1, I'm not gonna be able to give them values. I'm just gonna say mod1, mod2, and mod3. I've got three new variables and I want to find a way to export all three of these so that I can require them somewhere else.

[00:04:53] So let's go through some different strategies doing that. So I could just say, exports.mod1 = mod1 and so forth and so forth exports.mod2 = mod2, exports.mod3 = mod3, that'll attach them to an object. I could also not just not put var here, and just say exports.mod1 = something.

[00:05:22] Exports. Oops. .mod2 = something.
>> Speaker 2: The export has to go along with your require call?
>> Speaker 1: No, I'll get there. So, if we do this exports here, this will allow us to enter another file. So this is another file down here. IN another file, we'll be able to do, and imagine, this file's called mod.js.

[00:05:54] This is mod.js. And then another file in app.js. They're right next to each other. I can say var mod = require the name of that file which is (./mod). So I'll just say mod. That is going to equal an object with mod1 on it, mod2 on it, and mod3 on it.

[00:06:27] That's because I attached them to exports, and exports itself is an object. So when I say exports this, exports.this, exports.this, it attaches it to an object. So now if I want to access those modules, I would have to say mod.mod1 and mod.mod2 because-
>> Speaker 2: So is exports supplied by node?

>> Speaker 1: Yeah, it's supplied by node. Yeah, so again, if we, this iffy right here that's on every single file and node. So we have module, we have exports, yeah, we have dirname.
>> Speaker 2: Okay.
>> Speaker 1: Yeah, yeah we've got all that stuff, it's all in there, and there's also a module.exports, which is not exports.

[00:07:14] So this is exports. So, the way I figure this, everything that's on exports, just think of exports as the thing you're gonna require. When you use the exports keyword in one file, that's gonna be referenced as whatever variable you call the next file. So this is exports right here now, this is now exports.

[00:07:30] We just called in mod in the other file. So you see, it's the same thing. This mod is this object's exports. It's just an object. So now we can say mod.mod1, just like we said exports.mod1. It's the same object when you put things on exports like that. By using the exports, we're allowed to export as many things as want out this file.

[00:07:51] Module.exports does something like this. So, say we want the same behavior, we want to export an object, but we wanna use module.exports. So, module.exports means nothing else is gonna be exported from this file. This is it. This is the only thing that's gonna be exported from this file.

[00:08:08] And it can be anything we want, an object, a number, a function, whatever we want, but it's gonna be the only thing that's gonna be exported from this file. So, if we want the same behavior, we do the same thing, mod1,
>> Speaker 1: mod2
>> Speaker 1: And then mod3.
>> Speaker 1: And that would give us the same thing below.

[00:08:31] This'll still work the same way. This'll still be an object, and we still have these properties on it, and that's because we actually made it an object. It's whatever this value is, it's an object. If I was like, module.exports is a function now.
>> Speaker 1: Which we will do later on.

[00:08:48] Module.exports is a function, okay, this is no longer an object. It's a function.
>> Speaker 1: And because it is a function, I can just immediately invoke it like that cuz it's a function. So this thing took in some arcs, nums, I can just pass in some nums here.
>> Speaker 1: Or I could just say, okay, var func = that.

>> Speaker 1: And then call it.
>> Speaker 1: Does that make sense? Okay.
>> Speaker 1: Any other questions?
>> Speaker 2: In the subrouter, for example the tiger router in tiger JS, can you attach a middleware, like tiger router.use my middleware?
>> Speaker 1: Yes, you totally can, everything you could do in app, you could do here.

[00:09:50] The only difference is, it would only work on this router and children of this router. If this router used another router, it'll work there, too. So you could, if you had another router somewhere else, let's say we required another router and we were, so hey, tiger router.
>> Speaker 1: We want you to .use anything that comes into /tigers, /other, we want you to use the otherRouter.

[00:10:15] And then if we had some middleware up here on tigerRouter, some middleware, then now the other router uses that middleware too.
>> Speaker 1: Does use it, doesn't use it?