# 使用 NodeJS 建構前端網頁 (安裝) ## 本教學目標 * 安裝NodeJS。使用 NodeJS 擔任 Server * 運行程式範例 ## 什麼是NodeJS ?  Node.js 是一個開放原始碼、跨平台 JavaScript 程式實際執行環境,專注於伺服器端和網路應用系統。 Node.js 是後端技術,因此可讓開發者在應用系統伺服器端使用 JavaScript。 不過,Node 也提供專為前端開發而設計的模組和套裝軟體。 更新 ``` sudo apt update ``` 先檢查目前虛擬機版本 ``` lsb_release -a ``` 可以得知目前版本是 18.04  我們要先確認哪一個版本的nodeJS與Ubuntu版本是否相容 nodeJS nodesource: https://github.com/nodesource/distributions 發現18.04 只相容Node 16x,如果直接從官網下載最新版,可能會安裝失敗  ## 使用 NVM 管理node版本、npm版本 為了能夠方便管理node、npm的版本,會使用nvm這個套件做管理 下載NVM: ``` wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash ``` 執行NVM: ``` source ~/.bashrc ``` 使用NVM下載nodeJS,install 時直接安裝版本編號 16.16.0是LTS(長期服務)版 ``` nvm install 16.16.0 ``` 確認目前版本 ``` node -v npm -v ```  這樣就表示已經安裝完成,可以退出環境 顯示目前執行的PID環境 sudo lsof /var/lib/dpkg/lock-frontend  每個人PID process會不一樣,將其Kill -9(強制結束) sudo kill -9 7291 # 資料庫連線 本教學以範例為基礎讓學習者先順利執行範例程式,爾後進行調整修改。 下載git sudo apt install git 下載本次資料庫範例,將它放到VM端電腦 git clone https://github.com/dizzysoup/DBClass_Example clone 資料之後會出現DBClass_Example資料夾 cd DBClass_Example 安裝必要套件 npm install 在執行之前,先確定資料庫資料有連線成功 調整資料夾內的db.js資料庫連線(改成你的資料庫名稱) vim db.js vim 相關指令 i : 進入編輯模式 https://code.yidas.com/linux-vi-vim-command/  改好後,執行 node dbtest.js ,如果回傳DBConnectSuccess,代表連線成功  執行程式 npm start 本程式可以在 http://<你的電腦IP>/author 看到資料庫範例  # 參考連結 & 衍生閱讀 架站好幫手 XAMPP https://phpnoteattw.blogspot.com/2017/07/xampp.html nodeJS 安裝 https://progressbar.tw/posts/291 ExpressJS 創建一個骨架網站 https://developer.mozilla.org/zh-TW/docs/Learn/Server-side/Express_Nodejs/skeleton_website
×
Sign in
Email
Password
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