Benben
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [NET101] 網路基礎概論(搭配 JS 實作練習) ###### Date : 2021 May. 03 - 2021 May. 03 #### 先來理解溝通是怎麼回事 - 溝通前 ( 亞當跟夏娃 ) 需要先確定雙方的 `發送` & `接收` 功能都沒問題,所以為什麼需要 3次 溝通前測試 ```sequence note left of 小明:發送 : ???;接收 : ??? note right of 小美:發送 : ???;接收 : ??? 小明 -> 小美 : 安安,在嘛 note right of 小美:發送 : ???;接收 : OK 小美 -> 小明 : 在唷 :D note left of 小明:發送 : OK;接收 : OK 小明 -> 小美 : 收到,太好了 note right of 小美:發送 : OK;接收 : OK ``` > 承上 ```sequence note left of 小明:發送 : OK;接收 : OK note right of 小美:發送 : OK;接收 : OK 小明 -> 小美 : 請你跟我約會! note right of 小美:... note left of 小明:剛剛測試發送 & 接收都 OK 呀 ``` > 這又是另一個故事了,一定是剛好小美去洗澡了。 - 建立溝通守則 ( 統一度量衡 ) - 標準化內容格式 - 分為 header & body - 用狀態碼準化結果 - 用動詞標準化 - 規模化溝通守則 ( 客製化的溝通 ) - 新增服務代號,一人負責一個服務 - 不同服務可以有不同格式 - 有些服務不一定要經過三次確認 - 誇校傳紙條寫校名不用寫地址也行 - 為什麼我們需要協定 ( protocol ) - 需要一個溝通標準 - 有標準才可以規模化 #### HTTP ( Hyper Text Tansfer Protocal 超文本傳輸協定 ) - request 的一生 1. 瀏覽器傳 HTTP request 到 sever 2. serve 回傳一個 respones 3. 重點是裡面的格式 - DNS ( Domain Name System ) 1. github.com 的 Adress 是哪 ( CLI : `nslookup github.com` ) 2. 回傳 Address: 52.192.72.89 - local host 127.0.0.1 - 盜版原理 : request -> 導到不存在的 127.0.0.x -> XXX ( 沒有 respone ,也就無法驗證正版) - 瀏覽器也只是個程式 - 套件 : [Request - Simplified HTTP client]() - 安裝 : `npm install request` - 使用方式 : ```javascript const request = require('request'); request('http://www.google.com', function (error, response, body) { console.error('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // Print the HTML for the Google homepage. }); ``` - Header ```javascript const request = require('request'); request( 'http://www.google.com', (error, response, body) => { // 印出 header console.log(response.headers) } ) ``` - 常見 HTTP Method - GET, HEAD, POST, DELETE, PUT ( 取代 ) , PATCH ( 修改 ) - 常見的 HTTP Status code | code | 狀態 | meme | | --- | --- | --- | | 1XX | 訊息 | Hold on | | 2XX | 成功 | Here you go | | 3XX | 重新導向 | Go away | | 4XX | 用戶端錯誤 | You fucked up | | 5XX | 伺服器錯誤 | I fucked up | - 實作 : 簡易 Sever ```javascript // node 內建套件 let http = require('http') let server = http.createServer(handleRequest) // 處理 request, respones function handleRequest(req, res) { console.log(req.url) if (req.url === '/hello') { res.write('hello') res.end() } if (req.url === '/'){ res.write('welcome') res.end() } if(req.url === '/redirect'){ res.writeHead(302 , { 'Location' : 'https://google.com' }) res.end() } // 如果上面的路徑沒有,404 Error res.writeHead(404) res.end() } server.listen(5000) ``` #### TCP / IP - TCP / IP 網路層級 <table> <tr> <th colspan=2>OSI 模型</th> <th>TCP / IC 模型</th> <th>相關通訊協定</th> <tr/> <tr> <td>第七層 : 應用層</td> <td>Application</td> <td rowspan=3>應用層</td>> <td rowspan=3>HTTP, SSH, ...</td> </tr> <tr> <td>第六層 : 表示層</td> <td>Presentation</td> </tr> <tr> <td>第五層 : 會話層</td> <td>Session</td> </tr> <tr> <td>第四層 : 傳輸層</td> <td>Session</td> <td>傳輸層</td>> <td>TCP...</td> </tr> <tr> <td>第三層 : 網路層</td> <td>Network</td> <td>網路層</td> <td>IP...</td> </tr> <tr> <td>第二層 : 數據鏈路層</td> <td>Data Link</td> <td rowspan=2>網路訪問層</td> <td>LAN</td> </tr> <tr> <td>第一層 : 物理層</td> <td>Pysical</td> <td>WAN</td> </tr> </table> - IP 地址,各種 IP - IPv4, IPv6 - 固定 IP : 不會變 - 浮動 IP : 每次連線 IP 都不一樣 - 虛擬 IP : 只有內網裡面可以互通,對外是固定一個但不一樣的 IP - Port 的作用 - 讓網頁的程式監聽 ( 預設是 : HTTP 80, HTTPS 443, FTP 21) - 測試、開發常用的 Port : 3000, 4000, 8080, 8000 - TCP / UDP ( 兩種常見的傳輸層協定 ) - TCP 是可靠的連接,基本上 HTTP, FTP 都是建立在 TCP 上 - UDP 是相對不可靠,但是及時的連接,像是 : 視訊,即時比分 ... 等 > 補充 : TCP 三次握手 #### API ( Application Programming Interface ) - API 是甚麼 - 可分為 : 提供 API 跟 使用 API - 簡單說就是透過 API ,可以讓雙方交換資料 - Web API - Web API => HTTP API,透過 HTTP 協定的 API - SDK ( Software development kit ),包裝好的 API ,可以直接使用 - 例如 : 可以拿到寶可夢資料、隨機圖片、hacker-news - 串接 HTTP API 實戰 ```javascript const request = require('request') // node 內鍵模組,可以印出當前參數位置 const process = require('process') // process test // 印出參數位置 // console.log(process.argv) request( 'https://reqres.in/api/user/' + process.argv[2], function (err, res, body) { console.log(body) } ) ``` #### 資料格式的選擇 - XML ( Extensible Makeup Language ) - 歷久不衰,使用標籤當作 key,例如 : <id>benben</id> - JSON ( JavaScript Obejct Notation) - 熱門的資料格式,key & value 都要使用 `"` 包起來 - 可以使用 `const jsonObj = JSON.parse(body)` 將 JSON 轉成 Object - 可以使用 `const json = JSON.stringify(Obj)` 將 Object 轉成 JSON - SOAP ( Simple Object Access Protocol ) - 用 XML 交換資料 - RESTful API - 還不是協定,只是一種風格,請好好使用你的 API - API 串接實戰 ```javascript const request = require('request') // node 內鍵模組,可以印出當前參數位置 const process = require('process') // process test // 印出參數位置 // console.log(process.argv) request.patch( { 'https://reqres.in/api/user/2', form: { name: 'heillo' } }, function (err, res, body) { console.log(response.stautsCode) console.log(body) } ) ``` - 跳脫 HTTP 的限制 - 自訂義交換資料格式,不要被 potocol 侷限住 #### 好用工具 & 指令 - 必學 curl - `curl URL` : 發出 resquest ,可用 `|`, `>` 等指令串接 - `curl - I URL` : 只取得 header 內容 - 其他常用指令 - `ping URL` : 測試可不可以連到該網站 - `nslookup DNS/URL` : 解析 IP 位置、Doman name - `telnet 52.74.223.119 80` : 檢查 github 的 80 port 有沒有開 #### 結語 - 看穿網路的本質,才能不被迷惑 - 網路的本質是溝通、交換資料 - 基礎的 API 知識,使用 API --- #### 心得 課程前面的內容真的超級生動的 XDD 上課前真的覺得 Http 到底是 three 小,然後回憶高中時,真的是這樣傳紙條呢 ( 時代的眼淚 ),那時候還會把信紙折成好多種形狀。 我也好愛喝過氣的黃金比例翡翠檸檬呀!!! ( 幫清玉QQ ) 理論的地方講的很棒,不會太理論 (?) ,就是大多有點到,但更深入就太深了 QQ 後面的 API 實戰還蠻不錯的,自己做完有一點成就感,感覺熟悉之後可以做很多事,對,就是所謂的爬蟲 ( 爬蟲 = 串 API + 清洗資料 + 資料視覺化 ) ,又學到了許多知識。

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully