网络协议-全栈角度看HTTP协议(node.js)

目录

Method和解析Body

解析Body和2xx状态码

 调整Header和3xx状态码

实战-重定向观察

 错误处理4xx和5xx

实战-错误处理

小结:


Method和解析Body

解析Body和2xx状态码

// npm install express --save
// 安装nodemon(node的热更新)
const express = require('express')
const app = express()
// 打招呼接口
app.get('/greetings', (req, response) => {
    response.send('Hi!')
})
// fetch("/greetings")//模拟调用
//
app.post('/product', (req, res) => {
    const contentType = req.headers['content-type']
    let body = null
    
    console.log(contentType)// 服务端打印
    let requestText = ""
    req.on('data', (buffer) => {// 接收客户端传给后端的流
    	// 8bit - byte(8进制字节流)
       requestText += buffer.toString('utf-8')
       // console.log(buffer.toString('utf-8').length)  // gbk国标
    })
    req.on('end', () => {
        console.log(requestText.length) 
        switch(contentType) {
            case 'application/json' :
              body = JSON.parse(requestText)
              res.set('Content-Type','application/json')
    		   res.status(201).send(JSON.stringify({success:true}))
              break          
        } 
    })

})
// fetch('/product', {method: 'POST',headers:{'content-type':'application/json'} body:JSON.stringify({name:'apple'.padStart(1000000, 'A')})})

app.put('/product:id', (req, res) => {
    console.log(req.params.id)
    res.sendStatus(204)
})
// fetch('/product/123', {method: 'PUT'})

app.delete('/product:id', (req, res) => {
    console.log(req.params.id)
    res.sendStatus(204)
})
// fetch('/product/123', {method: 'DELETE'})

app.listen(3000, () => {})

 调整Header和3xx状态码

实战-重定向观察

观察下列重定向行为的区别(主要涉及爬虫,SEO优化)

  • 301
  • 302
  • 303
  • 307
  • 308
const express = require('express')
const app = express()

app.get('/301', (req, res) => { // 永久更新,会被记住,下次调也会跳到/def,哪怕你没有起这个服务
    res.redirect(301, '/def')// 接口重定向,目前浏览器会进行跳转重定向
})

app.get('/302', (req, res) => {
    res.redirect(302, '/def')// 接口重定向,目前浏览器会进行跳转重定向
})

app.post('/303', (req, res) => {
    res.redirect(303, '/def')// 接口重定向,目前浏览器会进行跳转重定向
})
// fetch('/303', method: "POST")
app.post('/302', (req, res) => {
    res.redirect(302, '/def')// 接口重定向,目前浏览器会进行跳转重定向
})
// 301,302,303可以post过去,get回来
// 307不能post过去get回来,得post过去,post回来

app.get('/303', (req, res) => {
    res.redirect(303, '/def')// 接口重定向,目前浏览器会进行跳转重定向
})

app.get('/def', (req, res) => {
    res.send('This is def(GET)')
})

app.listen(3000, () => {})

 错误处理4xx和5xx

实战-错误处理

为下列场景返回不同的错误码

  • 用户没有登录401
  • 服务器报错500
  • 内容没有找到404
  • 不支持POST方法405
const express = require('express')
const app = express()

app.get('bc', (req, res) => {
    res.sendStatus(401)
    // res.sendStatus(403)
    // res.sendStatus(404)
    // res.sendStatus(405)
    // throw "Error" // 500
})

app.get('/def', (req, res) => {
    res.send('This is def(GET)')
})

app.listen(3000, () => {})

小结:

Body解析过程中协商技巧很有用

switch(contentType) {
    case '' 
    break
}

认真对待3xx状态码

反向代理,负载均衡

错误处理遵循HTTP协议

网关,负载均衡

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值