Lesson 1 — Getting Started with NodeJS
Introduction
This is the first lesson in the course of “Fundamentals of web development”
“A journey of thousand miles begins with a single step” — Confucius
Teaching method
Experience has shown that students like hands-on experience with programming. The way I teach is with exercises, I try to come up with relevant and challenging tasks for students to solve. Each exercises has text and video solution. For the sake of simplicity, each solution includes only necessary code. This way students will not be overwhelmed by new information.
Objective of lesson 1
Upon successful completion of lesson one, student has learned how to install and use NodeJS and how to create executable JavaScript programs.
This article contains one exercise and few brain teasers.
In the upcoming articles, I will teach TypeScript, Webpack, React, Express, Heroku, ESLint, Jest.
Are you ready? Let’s get started.
Prerequisites: Basic understanding of variables and functions.
Exercise 1
Objective:
Write a Pounds (lbs) to Kilograms (kg) weight conversion calculator. Run JavaScript file from your Terminal.
Expected result:
Motivation: “If you are European and you want to survive in USA.”
Solution:
- Install Node.js on your machine https://nodejs.org/en/download/ Check if it was installed correctly in your terminal
node --version
My version is v14.9.0 - Create a new file ‘calculator.js’ with content:
console.log('Hello world');
3. Execute this file using a command node calculator.js
If “Hello world” appeared in your terminal, then everything is OK and cool! Otherwise you are in a big mess, start googling.
4. Find the formula for your calculator. (Hint: 1 lb = 0.45359237 kg)
5. Update “calculator.js” file. You need to ask input from the user. For that you can use readline
function. Read: “How do I prompt users for input from a command-line script?” https://nodejs.org/api/readline.html
6. Use ES6 syntax (ex arrow functions, constants, …) at http://es6-features.org/#Constants
7. Final code
const readline = require(‘readline’);const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});const convertPoundsToKg = (lbs) => {
const ONE_POUND_IN_KG = 0.45359237;
return lbs * ONE_POUND_IN_KG;
}rl.question(‘Pounds: ‘, (lbs) => {
const kgs = convertPoundsToKg(lbs);
console.log(“Kilograms: “, kgs)
rl.close();
});
8. Execute the file node calculator.js
9. The source code is available at Github https://github.com/programming-online-courses/lbs-to-kg/tree/lesson-1
Brain teasers:
It is important to understand the code, you should not memorize!
- What is
require('readline')
? Do we have to use it? Can you write previous solution without it? - What is an alternative syntax for
require
since ES2015? - Do you know how to create your own module and when you should do it?
- What is callback? When we need to use it? Can you write previous solution without it? What are alternatives for callback?
- What does
.close
function do? What happens if we don’t use it? Where you can read more about this function? - What is ECMAScript?
- If you try to access a variable, which is not defined, then the compiler will throw
ReferenceError
… We are usingprocess.stdin
, “process” is not defined in our file, why our code doesn’t crash? - Come up with your own bonus question and add it to comments under the article.
If you didn’t know all the answers, make sure you give a like to this article, then I will create a video with explanation, also if you want avoid embarrassing moment if a hot girl in bar asks you technical question.
Summary
You finished successfully lesson one, congratulations! :)
Go read my next lesson, You can find them on my website codegodzilla.com
You can also follow me on twitter.com/codegodzilla
Cheers 🍻