Node.js Taiwan Party 36 筆記 (屬雜記,請忽略我)

這偏是比較屬於個人雜記的部分,請忽略我...(應該只有我自己看得懂...)

Node.js TW 36th Party Note

Topic : Express

Starter need...

* node
* npm
* git
* grunt.js

Express → web base framework

express as cli

- e (ejs) -j(jhtml) -H(hogan)
- jade(as default)
- call help 
- c(css)
- s(session)  

express as css

- -css
- stylus (as default)

* why need template engine -> m”v”c 

run express cli

  • express [-option] [app_name]

folder structure

  • router (like controller + router)
  • static file (at Public folder)
  • app.js is init file

setting in Express

  • app.listen (open port 3000)
  • app.configure (read enviroment variable, ex: develpoment,production…)
  • app.set, app.get
  • app.enable, app.disable (ex, 不啓動view engine)
  • app.use
    • user --> Middleware --> route --> view
      middleware : app.use , will excute every route.
    
  • app.get
  • app.post
  • app.delete
  • app.update
  • app.all
  • app.locals (少用)

req get parameter

  • req.params(抓url)
    • req (像是讀檔案)
    • reponse (寫)
      app.get('/happymen/:id', function(req, res){
          var id = req.params.id;
      }); 
    
  • req.query (取get的資料)
  • req.body
  • req.files
  • req.cookies(read cookie) 重要。

req about HEADER (read only)

  • req.get, req.header (這兩個是相通的)
  • req.is (偵測)
  • req.ip, req.ips
  • req.host (req.subdomains)
  • req.protocol, (req.secure-->'https' == req.protocol; 檢查目前browser是http/https)
  • req.xhr (XMLHttpRequest)
  • Request is READ only in ExpressJS
  • req.status(res.statusCode) 設定header code要給多少(200,400,404,.....)
  • req.clearCookie
  • req.set, res.header
  • req.locals(app.locals is different)
    • life circle is only in url period (<--這好酷)
  • req.redirect (被設成302)
  • req.json, res.send (HEADER:application/json)
  • req.download
  • req.render

Response write data in Express

cross domain access

  • node.js 適合做 api,承載大量的連線 -> know how cross domain access

留言

這個網誌中的熱門文章

[Android] 筆記 手機上測試自己的 APP

解決fatal: Not a git repository (or any of the parent directories): .git錯誤

[Android 筆記] 設定 ImageView 的圖檔來源