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

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.
主站蜘蛛池模板: 蒙自县| 马鞍山市| 南靖县| 娄烦县| 历史| 鄯善县| 武定县| 天峨县| 嘉峪关市| 澜沧| 青神县| 樟树市| 隆尧县| 博湖县| 龙南县| 洪洞县| 平度市| 武安市| 武汉市| 滦南县| 安远县| 城口县| 陆丰市| 桐庐县| 宣威市| 肃宁县| 弋阳县| 岚皋县| 大田县| 久治县| 兴山县| 米易县| 壤塘县| 崇信县| 玉树县| 龙里县| 宝鸡市| 宝应县| 进贤县| 永福县| 汶川县|