馬
    • 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

      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.
      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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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

    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.
    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- title: 'Node.js' --- # Node.js ![](https://nodejs.org/static/images/logo.svg) --- Node.js 基於 Chrome V8 引擎的 JavaScript 執行環境 使用事件驅動,非阻塞型 I/O 套件管理工具是 ==npm==,是現在世界上最大的 open source 庫 <!-- .element: class="fragment" --> --- 簡單說 Node.js 就是用 JavaScript 來寫後端並編譯執行的環境 就像是 | 網頁伺服器 | 後端程式語言 | |---|---| | Apache | PHP | | IIS | C#, VB | | Node.js | JavaScript | --- ### 雖然是用 JavaScript 來寫程式,但這些都是後端用的,寫前端也需要懂這些? [Node.js](https://nodejs.org/en/) --- Node.js 問世,套件管理工具 npm 誕生開始 透過 npm,開發者便能快速且輕易的從 npm 下載到許多第三方套件<!-- .element: class="fragment" --> 前端開發人員可以用到這些套件來做轉譯程式碼、優化專案內的各種不同檔案、測試等等的用途<!-- .element: class="fragment" --> --- ### 優化 像是需要壓縮或合併 JS/CSS 來減少檔案的大小,降低開啟網頁的負載 --- ### 轉譯 編譯 ES6+ 的 JS 程式碼來相容低版本的瀏覽器 React 用的 ==JSX== Vue 用的 ==.vue== 檔 都要轉譯成瀏覽器看得懂的形式 自動辨別 CSS 程式碼的屬性來幫你加各家瀏覽器需求的前綴 (-webkit-、-moz-)<!-- .element: class="fragment" --> --- ### 自動化 自動化打包,只要先寫好設置或腳本 依照腳本不同,可以用來區隔開發時的程式碼及產品發布時的程式碼 --- 這些工具在 npm 上通通都找得到 並且開源可隨便你用<!-- .element: class="fragment" --> 所以說 Node.js 的出現是前端工程化的重要里程碑<!-- .element: class="fragment" --> 接下來會簡單介紹 Node.js 安裝及執行<!-- .element: class="fragment" --> --- ## 首先要先下載安裝 --- 進入 https://nodejs.org/en/ 的官網 ![](https://cdn-images-1.medium.com/max/1000/1*8mG_lnxP-YRKdpO5IQ_x7Q.png) --- 下載完成後點開執行檔 ![](https://cdn-images-1.medium.com/max/1000/1*5mhsv6aIOkodrD0NqJ55Yw.png) --- 然後就是 Next -> 我同意 -> 選安裝路徑 ... 省略 ... 等待安裝 -> 完成 --- 安裝完成後 node 會自動新增進系統環境變數路徑 npm 也會隨附著一起安裝完成 ![](https://cdn-images-1.medium.com/max/1000/1*jczTeK5XHyBT-lvt58IMqQ.png) --- 簡單使用一次 node 來執行 index.js ![](https://i.imgur.com/uCb9dy5.png) --- node 指令所執行的 js 檔案可是在 Node.js 伺服器上執行的呢! 與在 browser 上跑的時候相比可是有很多核心運作的++環境變數++是不一樣的!<!-- .element: class="fragment" --> 像是 window,document 這些環境變數可是不存在於 Node.js 環境之中<!-- .element: class="fragment" --> --- 簡單實驗個 console.log(window); 看看 ![](https://i.imgur.com/HueIUAN.png) --- ### 那 Node.js 環境是可以用來做什麼呢? --- 在 Node.js 的官網上有一個 DOCS 的 link ![](https://i.imgur.com/vmwtscn.png) --- 在 Node.js 環境要玩的就是這一堆後端核心函式庫 ![](https://i.imgur.com/7N74Aos.png) https://nodejs.org/dist/latest-v12.x/docs/api/ --- 展示使用 http 的模組 ![](https://i.imgur.com/yX7ozWv.png) --- 剛剛提到了模組 在 index.js 的第一行的程式用了 require() ```javascript= const http = require('http'); ``` Node.js 的模組是基於 CommonJS 模組規範<!-- .element: class="fragment" --> https://zh.wikipedia.org/wiki/CommonJS<!-- .element: class="fragment" --> --- ![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/db/Npm-logo.svg/330px-Npm-logo.svg.png) --- ### npm (Node Package Manager) 它是 Node.js 預設提供的套件管理工具 ```bash= npm install <要安裝的第三方函式庫名稱> ``` 透過指令來下載 npm 上的第三方函式庫或模組 就像是 Linux 有 apt-get,C# 有 NuGet,PHP 有 Composer 等等的這些指令 --- npm 上的第三方函式庫可不僅僅只有 for 後端 許許多多前端常見的東西也通通都可以載得到 像是 JQuery、bootstrap、lodash...等等 只要透過 npm 來下載並使用這些前端或後端的工具<!-- .element: class="fragment" data-fragment-index="1" --> 就能夠更方便有效的管理專案內依賴的函式庫<!-- .element: class="fragment" data-fragment-index="1" --> --- ## npm 操作說明 --- 新建一個空白資料夾 D:\test,並進入目錄 然後輸入 ```bash= npm init -y ``` --- 此時目錄裡就會產生一個 package.json ![](https://i.imgur.com/yrYBSlA.png) --- 在這裡示範從 npm 載 react 的函式庫回來 ```bash= npm i react ``` ![](https://i.imgur.com/zxS78qp.png) **npm i** 是 **npm install** 的簡短寫法 --- 在這裡示範從 npm 載 eslint 的函式庫回來 指令增加了 -D 的參數 ```bash= npm i -D eslint ``` ![](https://i.imgur.com/wqsrYDO.png) **npm i -D** 是 **npm install --save-dev** 的簡短寫法 --- 此時 test 目錄底下會多出另外兩個檔案 ![](https://i.imgur.com/7pq2RIz.png) 大多數情形不用特別開啟這兩個東西來查看 --- 打開 package.json ![](https://i.imgur.com/QQyPq0I.png) --- ### 放到 dependencies 或是 devDependencies 有什麼差嗎? --- 在前端的專案配置上 dependencies 主要是存放 client 端(瀏覽器上)使用的函式庫<!-- .element: class="fragment" --> 例:JQuery、bootstrap、lodash<!-- .element: class="fragment" --> devDependencies 主要是存放 Node.js 環境使用的函式庫<!-- .element: class="fragment" --> 例:打包工具 webpack、Coding Style 檢查工具 eslint<!-- .element: class="fragment" --> --- 掌握了 npm 的使用操作後 接下來要講解 ES6(ECMAScript 6)的部分 --- ###### tags: `前端` 下一章: [ES6](https://hackmd.io/p/H13__96nE#/)

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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