目錄:
一、常用 Command Line
二、Node.js
- REPL (Read Eval Print Loop / 互動式解釋器)
- 內建模組 Native Node modules
- 外載模組 NPM packages (Node Package Manager)
三、Express
- 建立Local Serve
- 設定訪問(get)瀏覽器後的response
- post: 讓server取得資料
四、nodemon: 可以讓server即時更改的package
一、常用 Command Line
$ ls
$ ls -a (檢視隱藏檔案)
$ cd
$ pwd
$ clear
$ mkdir DOCUMENT
$ touch index.js
$ start index.js
$ node server.js
$ rm server.js
$ rm -rf DOCUMENT
$ atom .
$ start atom index.js
// ctrl + a (move cursor to beginning)
// ctrl + e (move cursor to end)
// ctrl + c (cancel)
// ctrl + u (delete)
二、Node.js
01. REPL (Read Eval Print Loop / 互動式解釋器)
// 在terminal上執行js
$ node index.js
// 直接以terminal做console(控制台)
$ node
02. Native Node modules 內建 node 模組
- 可直接在atom中編輯
- 執行: node index.js
// eg. copy file
// module: fs.copyFileSync(file source, destination)
const fs = require('fs');
fs.copyFileSync("file1.txt", "file2.txt");
03. NPM packages (Node Package Manager) 外部模組
- 未使用過NPM之檔案需初始化:
npm init -y
- 安裝package:
npm install %package%
- 執行:
node index.js
// 初始化 NPM (on terminal)
$ npm init -y
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help init` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (intro-to-node)
version: (1.0.0)
description: This is a intro to node project
entry point: (index.js)
test command:
git repository:
keywords:
author: Penny Lee
license: (ISC)
About to write to C:\Users\Penny\Desktop\intro-to-node\package.json:
Is this OK? (yes)
// eg1. superhero npm: https://www.npmjs.com/package/superheroes
$ npm install superheroes
var superheroes = require("superheroes");
var name = superheroes.random();
console.log(name);
// eg2. supervillains npm: https://www.npmjs.com/package/supervillains
$ npm install supervillains
const supervillains = require("supervillains");
names = supervillains.all;
randomName = supervillains.random();
console.log(randomName);
三、Express
- 未使用過NPM之檔案需初始化:
npm init
- 在terminal上安裝Express:
npm install express
- 執行:
node index.js
01. 建立Local Server
const express = require("express");
const app = express();
app.listen(3000, function(){
console.log("Server started on port 3000")
});
// terminal上執行
$ node index.js (or nodemon server.js)
- 建立Local的Server(app)
- 讓server接通(listen)channel(3000)
- 執行後可以browser進入route(
localhost:3000/
) - 此時尚未設定server遭訪問時的回應,故會產生以下畫面:
02. 設定訪問(get)瀏覽器後的response
// 在app.listen前插入
app.get("/",function(req, res) {
console.log(req);
res.send("hello");
})
app.get("/about",function(req, res) {
// absolute path
res.sendFile(__dirname + "/about.html");
})
$ node index.js (or nodemon server.js)
- get function設定: 瀏覽器訪問後的回應
- first input: 造訪的網頁(target route),
"/"
表示主頁,"/about"
表示子頁面 - second input: 回應函式,自帶req & res參數
function(req, res)
- 可以針對req設定: eg.
console.log(req);
- 亦可針對res設定: eg.
res.send("<h1>HTML code</h1>");
- 也可以附帶html檔案作為response,但須標示absolute path:
res.sendFile(__dirname + "/index.html");
03. post: 讓server取得資料
- 未使用過NPM之檔案需初始化:
npm init
- 在terminal上安裝body-parser:
npm install body-parser
在index.js上載入
body-parser
const express = require("express"); const bodyParser = require("body-parser"); const app = express(); // pick data comes from HTML form // extended: true - allow us to post nested objects app.use(bodyParser.urlencoded({extended: true}));
呼叫
post request
app.post("/", function(req, res){ // .body 從HTML上取得資料(input) console.log(req.body); // > { num1: '22', num2: '33', submit: ''} var first_num = Number(req.body.num1); var second_num = Number(req.body.num2); var result = num1 + num2; res.send("Calculate result: " + result) })
- 執行
node index.js
四、nodemon: 可以讓server即時更改的package
- 安裝
$ npm install -g nodemon
- 使用
$ nodemon server.js