My Programming Progress: Day Two of the Complete Node.js Developer Course - require

in #javascript7 years ago (edited)

photo4985562535453370825 Cropped.jpg

Importing functions I've written from other .js files

On my first day, it was a lot of initial setup and review and then some learning how to import, or require, modules built into node. Today, I'm getting into things that I haven't done before.

Today, I've only gone through one section so far: 10. Requiring Your Own Files

I'll be focusing on that and sharing the exercise code I wrote. As is my habit, I've changed the code slightly to experiment with things and make sure that I understand the concepts. It will be a bit different than the code seen in the course video.

Requiring my own files

So this builds on what I learned yesterday when I required the built-in node modules os and fs with syntax like this:

const fs = require('fs');

Node.js knows that this is a built-in module and just brings it in when using require. If I've built some of my own functions and have saved them in a separate file, I can also require them using similar syntax:

const notes = require('./notes.js');

This difference is that the require statement bringing in my own file uses a relative path starting with ./ if the file is in the same directory as the javascript file I'm currently writing.

This section had a little challenge:

Have a function in another file that adds two numbers and then use that function in the main node app.

I'm happy to have been able to write it quickly. It's a simple challenge, but being able to quickly complete it without skipping ahead or looking for the answer shows that the javascript knowledge that I've gained in the last few months has stuck with me (even after taking a break for a few weeks). I didn't have any problems with syntax or programming logic.

Screenshot

Then here's the code from the notes.js file that I required:

Screenshot from 2017-08-29 22-43-31.png

I've included screenshots here from the code so you can see the syntax highlighting colors from the Atom text editor. In these posts, I may switch back and forth between screenshots and just using textual code.

Now, here's a screenshot of the result when I run the node app in the terminal:

Screenshot from 2017-08-29 22-48-34.png

This shows that I've successfully brought the add function in from my notes.js file and that it prints the correct sum out to the console (terminal). You can see that it uses "dot notation" with the variable name "notes" from my require statement:

notes.add(n1, n2)

I won't go into explaining the code line by line, as this is a simple example. If you have questions, don't hesitate to ask me in the comments.

As the work in the course becomes more difficult and complicated, I'll have to find a way to explain what I'm learning in an engaging way that's not too long. These posts aren't meant to re-teach what I'm learning in the course, as if you'd like to gain the skills presented in the course... it might be best to take the course. (smiley-face-emoticon)

To sum it up:

This section was giving an introduction to storing functions I've written in a separate file and then calling them in with "require". This is so that the code of larger projects can be separated out into logical sections. It would be hard to work with if all of the code for a larger project was just in one long javascript file.

Overview of these posts:

Previously:

The course I'm taking is The Complete Node.js Developer Course (2nd Edition) available on Udemy. I paid for the course with money earned here on Steemit!

I'm taking it because I want to learn how to build apps that persistently store data. My short term goal is to build a web app that keeps track of the books that each member of my family reads. My longer term goal is to develop the skills needed to work remotely as a web (and maybe blockchain) developer.

Thanks for reading!

--- @matthewdavid

Sort:  

I'm currently doing the same course. It's pretty good so far. I highly recommend it.

Good to hear that you recommend it!

Coin Marketplace

STEEM 0.20
TRX 0.13
JST 0.030
BTC 65733.39
ETH 3506.40
USDT 1.00
SBD 2.51