查看返回状态
跳转Header和3xx状态码
重定向观察
const express = require('express')
const app = express()
app.get('/301', (req, res) => {
res.redirect(301, '/def')
app.get('/302', (req, res) => {
res.redirect(302, '/def')
app.get('/303', (req, res) => {
res.redirect(303, '/def')
app.post('/302', (req, res) => {
res.redirect(302, '/def')
app.post('/307', (req, res) => {
res.redirect(307, '/def')
app.get('/def', (req, res) => {
res.send("This is def(GET)")
app.listen(3000, () => {console.log('listen on 3000')})
fetch('/301', {method: 'GET'})
get/301 => get/def 永久重定向
fetch('/302', {method: 'GET'})
get/302 => get/def
fetch('/303', {method: 'GET'})
get/303 => get/def
fetch('/302', {method: 'POST'})
post /302 => get/def
fetch('/307', {method: 'POST'})
post /307 => post/def 没有post/def 接口,返回404
新增 post/def接口,再来试一下
app.post('/def', (req, res) => {
res.send("This is def(POST)")
错误处理:4xx和5xx
app.get('/abc', (req, res) => {
res.sendStatus(404)
app.get('/abc', (req, res) => {
throw "Error"
app.get('/abc', (req, res) => {
res.sendStatus(502)
