req.xxx API 讲解

2022-02-22  本文已影响0人  kzc爱吃梨

request.xxx

概况:共28个API
需要特别学习的API

//  localhost:3000/user/1
app.get('/user/:id', (request, response, next)=> {
    console.log('request.params')
    console.log(request.params);
})
image.png
//  localhost:3000/user/1?name=kong&age=18
app.get('/user/:id', (request, response, next)=> {
    console.log('request.params')
    console.log(request.params);
})
image.png
//  localhost:3000/user/1?name=kong&age=18 
//  localhost:3000/user/kong

app.get('/user/:name', (request, response, next)=> {
    console.log('request.param')
    console.log(request.param('name'));
    next()
})
image.png

单词记忆

image.png

response.xxx

概况:共24个API
需要特别学习的API

app.get('/test', (request, response, next)=> {
    response.set('X-Kong', '123')
    response.append('X-Kong1', '456')
    response.status(401)
    response.send('hi')
    next()
})
image.png
res.format({
  'text/plain': function () {
    res.send('hey')
  },

  'text/html': function () {
    res.send('<p>hey</p>')
  },

  'application/json': function () {
    res.send({ message: 'hey' })
  },

  default: function () {
    // log the request and respond with 406
    res.status(406).send('Not Acceptable')
  }
})
image.png
app.get('/test', (req, res, next) => {
    // res.status(301)
    // res.location('/kong')
    res.redirect('/kong')  // 相当于上面两句话
    res.end()
    next()
})

app.get('/kong', (req, res, next) => {
    res.send('重定向地址')
    next()
})
image.png

单词学习

image.png

router.xxx

概况:共5个API
需要特别学习的API

app.use('/users', router)
const express = require('express')
const router = express.Router()

router.get('/', (req, res, next)=> {
    res.send('/user')
    next
})

router.get('/:id', (req, res, next)=> {
    res.send('/user/:id')
    next
})

router.get('/:id/edit', (req, res, next)=> {
    res.send('/user/:id/edit')
    next
})
image.png image.png
上一篇 下一篇

猜你喜欢

热点阅读