[Node.js] 學習筆記:安裝 Node.js 與學習 NodeSchool 課程

[Node.js] 學習筆記:安裝 Node.js 與學習 NodeSchool 課程

最近 Node.js 真的是相當的火紅,常常聽到它的大名,

可是卻不知道是什麼東西…

正好最近公司開了個 Node.js 的讀書會,藉著這個機會開始學習吧~

(我們讀的是 深入淺出 Node.js 這本書)

 

Node.js 的官網 可以看到下載點與相關的文件說明~

安裝方式也相當的簡單,

在 https://nodejs.org/download/ 這頁可以下載各平台的安裝檔,

或是用套件管理員也都可以支援~

像我在 Mac 上面就可以用 Homebrew 來安裝:

brew install node

 

裝好之後就可以用 node <js file> 來執行 JavaScript 檔案了~

像是可以先把 Node.js 的官網 上的範例直接複製下來存到一個檔案裡,

例如下面是一個 web server 的簡單範例:

var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

 

用 node 來執行這個檔案,可以看到 console.log() 會將訊息顯示在 terminal window 上面,

而用瀏覽器打開 http://127.0.0.1:1337 時,也可以看到 Hello World 的訊息: 

testuser@localhost ~ $ node webserver.js
Server running at http://127.0.0.1:1337/

 

在官網上面有個 tutorial 的頁面,裡面介紹了 NodeSchool 這個學習課程~

你可以用 npm 裝好這個套件後,就能用這套件來學習 Node.js 了,相當的貼心~

這個課程會需要自己寫出程式,再讓套件在驗證自己的程式有無問題,

這種立即上機練習的方式感覺很不錯!

testuser@localhost ~ $ sudo npm install -g learnyounode
LEARN YOU THE NODE.JS FOR MUCH WIN!
─────────────────────────────────────
HELLO WORLD
Exercise 1 of 13
Write a program that prints the text "HELLO WORLD" to the console (stdout).
-------------------------------------------------------------------------------
## HINTS
To make Node.js program, create a new file with a .js extension and start writing JavaScript! Execute your program by running it with the
node command. e.g.:
$ node program.js
You can write to the console in the same way as in the browser:
console.log("text")
When you are done, you must run:
$ learnyounode verify program.js
to proceed. Your program will be tested, a report will be generated, and the lesson will be marked 'completed' if you are successful.
-------------------------------------------------------------------------------
» To print these instructions again, run: learnyounode print
» To execute your program in a test environment, run: learnyounode run program.js
» To verify your program, run: learnyounode verify program.js
» For help run: learnyounode help

 

舉例來說,第一堂課要求的是寫出一個程式,可以秀出 HELLO WORLD 這個字串。

我先編寫好一個 JavaScript 檔案:

console.log("HELLO WORLD");

 

先執行 learnyounode run <filename> 來看一下執行結果:

testuser@localhost ~ $ learnyounode run 01_hello_world.js
HELLO WORLD

 

沒問題的話,就改用 learnyounode verify 的指令,來交作業囉~

順利的話,就可以完成一個題目,剩下 12 個:

testuser@localhost ~ $ learnyounode verify 01_hello_world.js
Your submission results compared to the expected:
ACTUAL                                 EXPECTED
────────────────────────────────────────────────────────────────────────────────
"HELLO WORLD"                       ==    "HELLO WORLD"
""                                  ==    ""
────────────────────────────────────────────────────────────────────────────────
✓ Submission results match expected
# PASS
Your solution to HELLO WORLD passed!
Here's the official solution in case you want to compare notes:
────────────────────────────────────────────────────────────────────────────────
    console.log("HELLO WORLD")
────────────────────────────────────────────────────────────────────────────────
You have 12 challenges left.
Type 'learnyounode' to show the menu.

 

NodeSchool 這個上機練習感覺蠻不錯的,推薦初學者來試試看囉~

我也要來繼續把剩下的 12 題都破關囉~^^ 

 

 

(本頁面已被瀏覽過 246 次)

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料