Express 總結(jié)

Express 基本概念

子應(yīng)用

var app = express() // 主應(yīng)用
var admin = express() // 另外一個應(yīng)用
app.use('/admin', admin) // admin 作子應(yīng)用

掛載點

  • "/admin" 就是 admin 的掛載點

Express API 總結(jié)

express.xxx - 內(nèi)置中間件

json([option])

內(nèi)置中間件,解析傳入的請求

app.use(express.json())
app.use((request, response, next) => {
    const jsonObj = request.body 
})

static(root,[option])

內(nèi)置中間件為,創(chuàng)建一個靜態(tài)文件服務(wù)器,根據(jù) req.url 來尋找對應(yīng)的文件

text([option])

內(nèi)置中間件,將傳入的請求參數(shù)解析為字符串

Router([option])

創(chuàng)建一個 router 對象,對路由進行響應(yīng)

app.xxx - 應(yīng)用設(shè)置

app.set(name , value)

存儲一個變量

app.get(name)

獲取變量

app.get("/xxx", fn)

對 get /xxx 的響應(yīng)

app.post / put /delete

對對應(yīng)的的請求方法進行響應(yīng)

app.use([path, ] callback [, callback])

使用中間件進行全局的處理
對應(yīng)的路由進行響應(yīng)

request.xxx - 操作請求

request.baseUrl

路由的 URL 路徑

request.body

請求體

request.params

/user/:name 中 name 變量

request.path

example.com/user?id=1 => "/user"

request.query

/seatch?a=1&b=2 req.query.a => 1

  • request.get() // 獲取請求頭中信息

  • request.param()

    // ?name=tobi
    req.param('name')
    // => "tobi"  
    

respones.xxx - 操作響應(yīng)

send([body])

res.send({ some: 'json' })

render(view [, locals] [, callback])

渲染view, 同時向callback 傳入渲染后的字符串

res.render('user', { name: 'Tobi' }, function(err, html){
  // ...
});

status()

設(shè)置狀態(tài)碼

set()

設(shè)置響應(yīng)頭

res.set({
  'Content-Type': 'text/plain',
  'Content-Length': '123',
  'ETag': '12345'
})

format()

設(shè)置特定請求頭的響應(yīng)

res.format({
  'text/plain': function(){
    res.send('hey');
  },
  'text/html': function(){
    res.send('hey');
  },
  'application/json': function(){
    res.send({ message: 'hey' });
  }
})

get()

返回一個大小寫不敏感的響應(yīng)頭的值

res.get('Content-Type');
// "text/plain"

router.xxx - 操作路由

router.all(path, [callback, ... ] callback)

與 methods 相同,適配所有的方法

router.methods(path, fn)

get/post/put/delete/update 對應(yīng)的請求方法進行響應(yīng)

router.param(name, callback)

構(gòu)造一個參數(shù)觸發(fā)器,根據(jù)參數(shù)出發(fā)回調(diào)
即使參數(shù)在多個路由中匹配,在請求-響應(yīng)周期中也僅會調(diào)用一次參數(shù)回調(diào)

router.param('id', function (req, res, next, id) {
  console.log('CALLED ONLY ONCE')
  next()
})

router.get('/user/:id', function (req, res, next) {
  console.log('although this matches')
  next()
})

router.get('/user/:id', function (req, res) {
  console.log('and this matches too')
  res.end()
})
/*print
CALLED ONLY ONCE
although this matches
and this matches too
*/

router.use([path], [function], funtion)

中間件的使用或者對路由的響應(yīng)

router.route(path)

返回單個路由的實例,然后您可以使用該路由使用可選的中間件來處理HTTP動詞。使用router.route()以避免重復(fù)請求的響應(yīng)

router.route('/users/:user_id')
  .all(function (req, res, next) {
    // runs for all HTTP verbs first
    // think of it as route specific middleware!
    next()
  })
  .get(function (req, res, next) {
    res.json(req.user)
  })
  .post(function (req, res, next) {
    next(new Error('not implemented'))
  })
  .delete(function (req, res, next) {
    next(new Error('not implemented'))
  })
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容