1. 程式人生 > >node——小坑、小技巧

node——小坑、小技巧

1、express中res.end 、res.send、res.json返回資料的區別

      用於快速結束沒有任何資料的響應,使用res.end()。 
      響應中要傳送JSON響應,使用res.json()。 
      響應中要傳送資料,使用res.send() ,但要注意header ‘content-type’引數。 
      如果使用res.end()返回資料非常影響效能。

2、body-parse

使用body-parse來解析POST傳遞過來的資料時,在app.js檔案中新增
app.use( bodyParser.urlencoded({extended: true}) );//解析application/x-www-form-urlencoded
app.use(bodyParser.json());//解析json資料依賴於urlencoded模組 必須同時應用
app.use(bodyParser.text());//將所有的資料以文字格式字串的返回 form表單中 text-plain
app.use(bodyParser.raw());//解析二進位制資料

//設定完畢之後,會在req物件上面新增一個req.body的一個物件