Node.js——express

  • res.send(),比原生的 res.end() 強大,原生只支持字符串和Buffer對象,並且須要本身加響應報文頭,send支持字符串、Buffer、Json對象、數組,並且自動加響應報文頭
  • app.use('/index',function(req,res){}) use請求,不在意請求方式是get 仍是 post 仍是其餘,並且只要是以 /index 開頭就是知足條件。注意 /indexxxx 這樣是不行的
  • app.all('/index',function(req,res){})   all請求,不在意請求方式,可是請求路徑必須嚴格等於
  • app.get、app.post、app.patch.......是屬於嚴格等於的範疇,必須與請求路徑一致並且請求方式也是同樣,固然能夠使用正則,app.get('/^\/index(\/.+)*$/'),這樣就能夠匹配以/index開頭的get方法
  • app.get('/index/:year/:month/:day'),經過req.params 獲取路由中的參數
  • app.use('/',express.static(path.join(__dirname,'public'))),開放靜態資源
  • res.redirect() 重定向
  • res.json() 返回一個json對象,同res.send()
  • res.sendFile() 返回一個文件
  • res.status(200).end() 比以前的快多了
相關文章
相關標籤/搜索