SlideShare a Scribd company logo
1 of 18
Download to read offline
NODE.JS實作筆記(1)
-以EXPRESS+MONGODB搭建簡單部落格

Jen-Hsuan Hsieh (Sean)
2015/10/10
INTRODUCTION:
NODE.JS
!None block and asynchronized I/O
-Advantage:
-適合IO密集型操作
-單線程
-使⽤用Javascript
-Disadvantage:
-⼤大量計算會導致堵塞
-Crash
INTRODUCTION:
EXPRESS(1/2)
Ÿ  Express是⺫⽬目前最穩定,使⽤用最廣泛開發框架
Ÿ  Express為HTTP模組提供了更高端的接口外還實現了許多功能,
其中包括:路由控制模組解析支持, 動態視圖, 用戶會話, CSRF
保護, 靜態文件服務, 錯誤服務器, 訪問日誌, 緩存, 插件支持
INTRODUCTION:
EXPRESS(2/2)
!Express is a routing and middleware web framework with minimal
functionality of its own: An Express application is essentially a series of
middleware calls.
!Middleware is a function with access to the request(req), the response
object(res) and the next middleware in the application’s request-
response cycle, commonly denoted by a variable named next.
!Middleware can:
-Execute any code.
-Make changes to the request and the response objects.
-End the request-response cycle.
-Call the next middleware in the stack.
Reference: http://expressjs.com/guide/using-middleware.html
系統規劃:搭建BLOG (1/2)
!功能規劃
-可註冊,登⼊入,發⽂文,登出
!目標確立
-未登錄:
-首頁:左側導覽頁顯示登入,註冊,右側顯示已發表的文章
-登入頁
-註冊頁
-已登錄:
-首頁:左側導覽頁顯示發文,登出,右側顯示已發表的文章
-發文頁
-登出頁
系統規劃:搭建BLOG (2/2)
!路由規劃
Main
Log in
Log out
Post
Register
middleware
Database
Insert
Get
VIEW
範本引擎:EJS標籤(1/2)
!分為三種:
-% code%: Javascript程式碼
-%= code%:替換為HTML
-%- code% :替換為原始HTML內容
範本引擎:EJS標籤(2/2)
!模組化相同的區塊
-維持⼀一致性
頁面存取控制權
!在middleware中加⼊入對登錄者的檢查功能
!註冊,登錄⾴頁⾯面只開放給未登⼊入的⽤用⼾戶
-checkNotLogin()
!發表⾴頁只開放給已登錄的⽤用⼾戶
-checkLogin()
!next(): 進行下一個流程
CSS
!Body
-margin: content的邊界
-padding:content內容的邊界
-em: equal to the font-size of the element in question
-border-bottom:下邊框的樣式
-solid:邊框線條樣式
!Reference:
https://developer.mozilla.org/zh-TW/docs/Web/CSS/margin
CONTROLLER
頁面通知(1/2)
!session
!透過對session的使用實踐對使用者狀態的檢測,再根據不同的使用
者狀態顯示不同的導覽資訊
!簡單來說,Session 的機制就像是你去飲料店下了單以後,得到號
碼牌,然後你走開幾步,店員就忘了你是誰。所以,如果你想去取
飲料,你就得靠這張號碼牌,去跟店員領,店員會跟據這號碼牌,
認定你是顧客、是否點過餐、知道你點了什麼東西,然後可以接著
給你屬於你的飲料。
Reference: Fred’s blog
(http://fred-zone.blogspot.tw/2014/01/web-session.html)
頁面通知(2/2)
Ÿ  connect-flash
The flash is a special area of the session used for storing
messages. Messages are written to the flash and cleared after being
displayed to the user. The flash is typically used in combination with
redirects, ensuring that the message is available to the next page that
is to be rendered.
Reference: https://github.com/jaredhanson/connect-flash
MODEL
MONGODB
!MongoDB的優點
-資料類型豐富
-⽂文件導向:”可⽤用單⼀一筆紀錄來表⽰示複雜的資料關係
-易於擴充
-⽔水平擴展:可將普通電腦加⼊入叢集,就可增加儲存空間及
增進效能
-許多的功能
-內儲Javascript,聚集,固定⼤大⼩小的集合,檔案儲存
-極致的效率
-⽤用⼀一種⼆二元溝通協定來跟伺服器溝通
-管理簡單
MONGODB
!啟動 MongoDB 資料庫:
sudo service mongodb start
!在 ~/.bashrc 中加⼊入:
export PATH=$PATH:/opt/mongo/bin
! CRUD
refererence: http://blog.gtwang.org/iot/raspberry-pi-install-
node-js-and-mongodb-database/
系統設計
Reg.ejs
Main.ejs
MongoDB
Success
message
Header.ejs
Footer.ejs
App.js
Error
message
Index.js
User.js
session
Style.css
Post.ejs
Login.ejs
LogOut.ejs
Post.js
View
Controller
Model

More Related Content

Similar to Node.js筆記(1)

GlassFish特性介绍
GlassFish特性介绍GlassFish特性介绍
GlassFish特性介绍Jim Jiang
 
Kiss开发平台简介
Kiss开发平台简介Kiss开发平台简介
Kiss开发平台简介cecepig
 
Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出Eric Xiao
 
Web2.0 attack and defence
Web2.0 attack and defenceWeb2.0 attack and defence
Web2.0 attack and defencehackstuff
 
全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版Simen Li
 
Ceph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China MobileCeph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China MobileCeph Community
 
Ceph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China MobileCeph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China MobileDanielle Womboldt
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型Jackson Tian
 
Grails:下一代Java Web开发框架
Grails:下一代Java Web开发框架Grails:下一代Java Web开发框架
Grails:下一代Java Web开发框架foxgem
 
Web开发与运维安全浅见
Web开发与运维安全浅见Web开发与运维安全浅见
Web开发与运维安全浅见mysqlops
 
浅析主流商业和开源Esb产品
浅析主流商业和开源Esb产品浅析主流商业和开源Esb产品
浅析主流商业和开源Esb产品nereuschen
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsHo Kim
 
PHP WEB 应用组织与结构
PHP WEB 应用组织与结构PHP WEB 应用组织与结构
PHP WEB 应用组织与结构HonestQiao
 
Node js feat pegasus
Node js feat pegasusNode js feat pegasus
Node js feat pegasuscnfi
 
Swsoft Prima@Hosting 介绍
Swsoft Prima@Hosting 介绍Swsoft Prima@Hosting 介绍
Swsoft Prima@Hosting 介绍webhostingguy
 
20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性
20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性
20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性liu sheng
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南yiditushe
 
Struts Mitac(1)
Struts Mitac(1)Struts Mitac(1)
Struts Mitac(1)wangjiaz
 
突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會
突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會
突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會Jason Cheng
 

Similar to Node.js筆記(1) (20)

GlassFish特性介绍
GlassFish特性介绍GlassFish特性介绍
GlassFish特性介绍
 
Kiss开发平台简介
Kiss开发平台简介Kiss开发平台简介
Kiss开发平台简介
 
Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出Node.js中间件 connect模块深入浅出
Node.js中间件 connect模块深入浅出
 
Web2.0 attack and defence
Web2.0 attack and defenceWeb2.0 attack and defence
Web2.0 attack and defence
 
全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版
 
Ceph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China MobileCeph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China Mobile
 
Ceph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China MobileCeph Day Beijing - Leverage Ceph for SDS in China Mobile
Ceph Day Beijing - Leverage Ceph for SDS in China Mobile
 
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
从无阻塞并行脚本加载(Lab.js)到浏览器消息模型
 
Grails:下一代Java Web开发框架
Grails:下一代Java Web开发框架Grails:下一代Java Web开发框架
Grails:下一代Java Web开发框架
 
Web开发与运维安全浅见
Web开发与运维安全浅见Web开发与运维安全浅见
Web开发与运维安全浅见
 
浅析主流商业和开源Esb产品
浅析主流商业和开源Esb产品浅析主流商业和开源Esb产品
浅析主流商业和开源Esb产品
 
PHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming SkillsPHP Coding Standard and 50+ Programming Skills
PHP Coding Standard and 50+ Programming Skills
 
PHP WEB 应用组织与结构
PHP WEB 应用组织与结构PHP WEB 应用组织与结构
PHP WEB 应用组织与结构
 
Node js feat pegasus
Node js feat pegasusNode js feat pegasus
Node js feat pegasus
 
Swsoft Prima@Hosting 介绍
Swsoft Prima@Hosting 介绍Swsoft Prima@Hosting 介绍
Swsoft Prima@Hosting 介绍
 
Jobforcompal
JobforcompalJobforcompal
Jobforcompal
 
20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性
20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性
20120613联动优势数据访问层DAL架构和实践4(刘胜)最新特性
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南
 
Struts Mitac(1)
Struts Mitac(1)Struts Mitac(1)
Struts Mitac(1)
 
突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會
突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會
突破困境:資安開源工具之應用分享 [2019/03/21] @臺灣資安大會
 

More from Jen-Hsuan Hsieh

More from Jen-Hsuan Hsieh (9)

觀察者模式
觀察者模式 觀察者模式
觀察者模式
 
轉接器模式與表象模式
轉接器模式與表象模式 轉接器模式與表象模式
轉接器模式與表象模式
 
獨體模式
獨體模式 獨體模式
獨體模式
 
裝飾者模式
裝飾者模式 裝飾者模式
裝飾者模式
 
策略模式
策略模式 策略模式
策略模式
 
狀態模式
狀態模式 狀態模式
狀態模式
 
命令模式
命令模式 命令模式
命令模式
 
反覆器模式與合成模式
反覆器模式與合成模式 反覆器模式與合成模式
反覆器模式與合成模式
 
工廠模式
工廠模式 工廠模式
工廠模式
 

Node.js筆記(1)