官术网_书友最值得收藏!

Activity: Running Basic Node.js Code

Before You Begin

Open the IDE and the Terminal to implement this solution.

Aim

Learn how to write a basic Node.js file and run it.

Scenario

You are writing a very basic mathematical library with handy mathematical functions.

Steps for Completion

  1. Create your project directory (folder), where all the code for this and other chapters will be kept. You can call it beginning-nodejs for brevity. Inside this directory, create another directory named lesson-1, and inside that, create another directory called activity-a. All this can be done using the following command:
mkdir -p beginning-nodejs/lesson-1/activity-a
  1. Inside activity-a, create a file using touch maths.js command.  
  2. Inside this file, create the following functions:
    • add: This takes any two numbers and returns the sum of both, for example, add(2, 5) returns 7
    • sum: Unlike add, takes any number of numbers and returns their sum, for example, sum(10, 5, 6) returns 21
  3.  After these functions, write the following code to act as tests for your code:
console.log(add(10, 6)); // 16
console.log(sum(10, 5, 6)); // 21
  1. Now, on the Terminal, change directory to lesson-1. That's where we will be running most of our code from for the whole chapter.
  2. To run the code, run the following command:
node activity-a/math.js

The 16 and 21 values should be printed out on the Terminal.

Even though you can configure the IDE so that Node.js code be run at the click of a button, it's strongly recommend that you run the code from the Terminal to appreciate how Node.js actually works.
For uniformity, if you are using a Windows machine, then run your commands from the Git Bash Terminal.

For the reference solution, use the math.js file at Code/Lesson-1/activity-solutions/activity-a.
主站蜘蛛池模板: 石阡县| 邓州市| 铜川市| 宣恩县| 德州市| 芒康县| 贵南县| 耿马| 河西区| 文成县| 南丹县| 萨迦县| 滦平县| 泰州市| 潜江市| 石城县| 曲松县| 永胜县| 洛川县| 孟津县| 株洲市| 远安县| 杭州市| 北川| 大兴区| 遂溪县| 德州市| 衡水市| 弥勒县| 天长市| 宁夏| 晋中市| 应用必备| 廊坊市| 盘锦市| 乐业县| 新巴尔虎左旗| 平山县| 桂阳县| 喀喇| 通城县|