- Node.js
Static Website | Dynamic Website |
---|---|
Content of Web pages can not be change at runtime. | Content of Web pages can be changed. |
No interaction with database possible. | Interaction with database is possible |
It is faster to load as compared to dynamic website. | It is slower than static website. |
Cheaper Development costs. | More Development costs. |
No feature of Content Management. | Feature of Content Management System. |
HTML, CSS, Javascript is used for developing the website. | Server side languages such as PHP, Node.js are used. |
Same content is delivered every time the page is loaded. | Content may change every time the page is loaded. |
- PHP
- Node.js + Express + MongoDB
- Java
- Ruby on Rails
- Python (Django)
- .NET
- ...
- Node.js allows us to run JavaScript outside the browser (Therefore, it’s not only a programming language for front-end development.)
- Module in Node.js is a simple or complex functionality organized in single or multiple JavaScript files which can be reused throughout the Node.js application.
- There are built-in modules in Node.js, but we can also make our own modules or use modules from other people.
- The Module Wrapper
- If we run JS code with Node.js, we can use modules and special Node.js syntax
- 參考文件: Nodejs v16 Module Wrapper Docs
- Before a module's code is executed, Node.js will wrap it with a function wrapper that looks like the following:
(function (exports, require, module, __filename, __dirname) { // Module code actually lives in here });
- It keeps top-level variables (defined with
var
,const
orlet
) scoped to the module rather than the global object. - It helps to provide some global-looking variables that are actually specific to the module, such as:
- The
module
andexports
objects that the implementor can use to export values from the module. - The convenience variables
__filename
and__dirname
, containing the module's absolute filename and directory path.
- The
製作一個 .js
檔案,並設定其 exports
function morning(myName) {
console.log("Good Morning " + myName);
}
exports.morning = morning;
用 require
導入
let try1 = require("./try1");
try1.morning("rjun");
創建一個 directory 並把要導入 app.js
的 .js 檔案都丟進去,
並且在裡面創建一個 index.js
將要用的 module 都導入到裡面
Index.js
let try1 = require("./try1");
let try2 = require("./try2");
exports.morning = try1.morning;
exports.sayHi = try2.sayHi;
app.js
let greeting = require("./greeting"); // 會自動讀取到 index.js
greeting.morning("rjun");
greeting.sayHi("rjun");
4.1 Path
The path
module provides utilities for working with file and directory paths. It can be accessed using:
const path = require("path");
Syntax | Description |
---|---|
path.basename(path[, ext]) |
returns the last portion of a path |
path.extname(path) |
returns the extension of the path , from the last occurrence of the . (period) character to end of string in the last portion of the path. |
path.join([...paths]) |
joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path. |
4.2 URL
4.3 File System
writeFile
import { writeFile } from "fs";
writeFile("message.txt", "Hello Node.js", "utf8", callback);
readFile
import { readFile } from "fs";
// macOS, Linux, and Windows
readFile("<directory>", (err, data) => {
// => [Error: EISDIR: illegal operation on a directory, read <directory>]
});
- IPv4 : 過去,IP 由 4 個 bytes 組成 ⇒ 32 bits ⇒ 2 ^ 32 ⇒ 42 億 9000 萬。不夠用!
- IPv6 : 現在,IP 由 16 個 bytes 組成 ⇒ 128 bits ⇒ 2 ^ 128
- DNS 伺服器將名稱請求轉換為 IP 地址
- 例如將 www.example.com 這種人們可讀取的名稱轉換為 192.0.2.1 等數字 IP 地址
- 網際網路 DNS 系統的工作原理和電話簿類似,管理名稱和數字之間的映射關係
這裡指邏輯意義上的 Port,一般是指 TCP/IP 協議中的 Port,Port 號的範圍從 0 到 65535,比如用於流覽網頁服務的 HTTP 預設使用 80 Port,用於 FTP 服務的 21 Port 等等
架一個本地伺服器(localhost),監聽 3051 這個 port
// 取得 http module
const http = require("http")
// 建 server
const server = http.createServer((req, res) => {
...
res.write("Hello")
res.end()
})
// 設定 port number
server.listen(3051, () => {
console.log("Server is running on port 3501.")
})
但通常不會這樣寫,大多數人會使用 Express 框架來寫 node server
- Stands for node package manager.
- It keeps tracks of what modules you get from outside.
- Npm was installed with Node.js
- 可以到 npmjs.com 找到別人做的 modules
- 每次要用別人的 modules 都要用 terminal 到當前位址輸入
npm init
做好設定
link → npm nodemon
好用的 module,當 js 檔案更新在 terminal 會自動更新