- Beginning API Development with Node.js
- Anthony Nandaa
- 291字
- 2021-07-23 17:21:47
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
- 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
- Inside activity-a, create a file using touch maths.js command.
- 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
- 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
- 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.
- 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.
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.
推薦閱讀
- 廣電5G從入門到精通
- C++黑客編程揭秘與防范
- 物聯(lián)網(wǎng)安全:理論、實踐與創(chuàng)新
- 正在爆發(fā)的互聯(lián)網(wǎng)革命
- 2018網(wǎng)信發(fā)展報告
- Mastering TypeScript 3
- 數(shù)字調(diào)制解調(diào)技術(shù)的MATLAB與FPGA實現(xiàn):Altera/Verilog版(第2版)
- 中國互聯(lián)網(wǎng)發(fā)展報告2018
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- 6G:面向2030年的移動通信
- LwIP應(yīng)用開發(fā)實戰(zhàn)指南:基于STM32
- Laravel Application Development Cookbook
- 數(shù)字王國里的虛擬人:技術(shù)、商業(yè)與法律解讀
- 網(wǎng)絡(luò)基本通信約束下的系統(tǒng)性能極限分析與設(shè)計
- 物聯(lián)網(wǎng)商業(yè)設(shè)計與案例