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

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.
主站蜘蛛池模板: 政和县| 南川市| 沙河市| 平远县| 永年县| 柯坪县| 白朗县| 沂源县| 舒城县| 长治县| 清镇市| 嵊泗县| 井陉县| 保亭| 宁乡县| 怀宁县| 体育| 盐边县| 沭阳县| 衡南县| 来宾市| 阿拉善右旗| 容城县| 惠安县| 黄大仙区| 常宁市| 呼和浩特市| 河南省| 台北县| 土默特左旗| 漳州市| 淮阳县| 宣汉县| 南郑县| 中卫市| 北辰区| 碌曲县| 东丽区| 察哈| 广饶县| 朝阳县|