珠峰-6-http和http-server原理

本文深入探讨HTTP协议的Header规范、状态码及请求方法,详解WebSocket的实现原理及其与HTTP的关系,同时覆盖了Restful API设计原则与传输数据流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

???? websock改天研究下然后用node去搞。 websock的实现原理。

##### 第9天的笔记内容。

## Header 规范

## Http 状态码
- 101 webscoket 双向通信
- 200 成功 204 没有响应体 206 断点续传
- 301(永久重定向) 302(临时重向) 304(缓存)只能服务端设置
- 401 (没有权限) 403 (登录了没权限) 404  405(请求方法不存在、不支持)
- 502 负载均衡 


## 请求方法 RestfulApi
根据不同的动作 做对应的处理
- get 获取资源 
- post 新增资源 
- put 上传文件  修改
- delete  删除资源
- options 跨域出现 (复杂请求时出现) 只是get / post 都是简单请求 + 自定义的header

## 传输数据
- 请求行 url
- 请求头 自定header
- 请求体 提交的数据

- 响应行 状态码
- 响应头 可以自定义
- 响应体 返还给浏览器的结果

## websocket 第一次通信是通过http + tcp

 

##### 第9天自己的总结

##### 模仿http测试接口, (1)命令行    (2) postman   (3)自己一个html    (4)自己 写一个node client.js

#####  自己写一个服务器。

// 通过node实现一个http服务
// 都是通过核心模块来提供  http模块
const http = require('http');

// 服务器要有特定的ip 和端口号
let server = http.createServer();
let querystring = require('querystring');
let url = require('url'); // url.parse

// 如果别人请求我,我需要去解析请求
server.on('request',(req,res)=>{
    // req 代表的是客户端
    // res 代表的就是服务端
    
    // 1) 请求行
    console.log(req.method); // 方法名大写
    let {pathname,query} = url.parse(req.url,true)
    console.log(pathname,query); // 请求路径  / 表示首页  url是不包含# hash的
    console.log(req.httpVersion);
    // 2) 请求头
    console.log(req.headers); // 取header都是小写
    // 3) 获取请求体
    let arr = []
    req.on('data',function(chunk){
        arr.push(chunk);
        console.log(chunk.toString()); // 流的原理 push(null) data方法不一定会触发
    });
    req.on('end',function(){
        console.log('end'); // end是一定触发
        console.log()

        // 响应行 头 体
        res.statusCode = 200; // 响应状态码
        // res.setHeader('Content-Length','1');
        res.setHeader('Content-Type','text/plain;charset=utf-8');
        let content = Buffer.concat(arr).toString();
        let type = req.headers['content-type']
        if(type === 'application/json'){
            let obj = JSON.parse(content)
            return res.end(obj.a+'');
        }else if(type === 'application/x-www-form-urlencoded'){
            // 第二个参数 字段间的分隔符 第三个参数 是key value分隔符
            let obj = querystring.parse(content);
            return res.end(obj.a+'');
            //  let str = 'a=1; b=2'
            //  str.replace(/([^=&])=([^&=])/g,function(){
            //      console.log(arguments[1],arguments[2])
            //  })
        }else{ //  如果不支持的就默认返回
            return res.end(content);
        }
       
    });
    
})

// 开启一个端口号
let port = 3001;
server.listen(port,()=>{
    console.log(`server start ${port}`)
});

// 如果端口被占用了 自动+1
server.on('error',(err)=>{
    if(err.errno === 'EADDRINUSE'){
        server.listen(++port)
    }
})

// 每次服务端代码发生变化 都需要重启服务
// nodemon node的监视器 监视文件变化的
// sudo npm install nodemon -g   nodemon 文件名  (可以增加配置文件)
// pm2





// 一个完整的url
// const requestUrl = `http://username:password@www.baidu.com:80/s?offset=1&limit=30#app`

// let result = url.parse(requestUrl,true);
// console.log(result)
/*
{
    protocol: 'http:',
    slashes: true,  是否有/
    auth: 'username:password', 用户信息
    host: 'www.baidu.com:80',
    port: '80',
    hostname: 'www.baidu.com', // 主机名
    hash: '#app',
    query: 'offset=1&limit=30',  查询参数
    pathname: '/s',  请求路径 资源路由
    path: '/s?offset=1&limit=30',
    href:
     'http://username:password@www.baidu.com:80/s?offset=1&limit=30#app' }

*/

 

 

 

 

 

 

##### form表单可以跨域。 

##### json用双引号

 

#####

保证this正确的三种方案。 

 

 

 

####__defineSetter__是defineProperty  的部分功能。

 

 ##### 作用域的问题,有了作用域重名的机会就少了

 

#### 说是@babel-polyfill已经可以被替代了,这个表示怀疑。

 

 #### 有时间观察下这个字段。

Connection:
keep-alive

 #### keep的头部

 

##### 状态码,返回状态。

 

 

##### options请求。

https://www.cnblogs.com/ermaoblog/p/8855915.html

出现的条件1: 跨域的时候才会出现

出现的条件2: 复杂请求。比如delete,put   和如果请求的是json,post也属于复杂请求.

#### form不会有跨域问题。

form允许跨域。一般提交到不同的域名之后,就跳走了,不跳走,当前页面的js页获取不到返回的信息。

https://blog.youkuaiyun.com/M_allstar/article/details/99663049

 

 

 

##### http-server原理。

 #### 对这句话存疑

 

 

##### 静态资源放在打包前打包后都能找到的公共地方。

 

  #### 强制缓存10秒会发起请求么。

强制缓存的文件会发起请求,但是结果返回的是200,

对比返回返回的是304.

 

#### 一般出现这个问题,代表浏览器没有发送成功。但是谷歌有时候,也是发送成功了。

 

#### 代理正向代理,反向代理。

Nginx,webpack都是反向代理。

跳板机正向代理。

反向代理一般对用户透明的,用户感觉不到,做一些负载均衡,服务端的保护墙。

而正向代理是客服端的保护墙。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/coding4/p/11613267.html

### Third-Party API DeepSeek Working Principle Third-party APIs like DeepSeek function as intermediaries that allow software applications to communicate with external services or data sources. For an API such as DeepSeek, the fundamental operation revolves around sending requests from a client application and receiving responses back from the server hosting the API. In terms of implementation details: 1. **Request Construction**: A request is constructed by specifying parameters required by the API endpoint. This includes headers, query strings, path variables, and body content depending on what type of HTTP method (GET, POST, PUT, DELETE etc.) is being used[^1]. 2. **Authentication & Authorization**: Before accessing certain resources through the API, authentication mechanisms ensure only authorized users can interact with protected endpoints. Common methods include OAuth tokens, API keys, or session-based logins. 3. **Data Exchange Formats**: Data exchanged between clients and servers typically follows standardized formats such as JSON or XML. These structures define how information should be packaged within each message sent over the network. 4. **Error Handling Mechanisms**: Robust error handling ensures graceful degradation when issues arise during communication. Status codes returned alongside any errors provide insight into whether operations succeeded or failed along with reasons why they might have encountered problems. For specific functionalities offered by DeepSeek related to augmented reality experiences including object identification for shopping guidance, interactive stickers based on face tracking technology among others mentioned previously[^4], these features likely involve advanced computer vision algorithms processing visual inputs captured via device cameras before returning relevant metadata about detected items or generating graphical overlays accordingly. ```python import requests def call_deepseek_api(endpoint, params=None): url = f"https://api.deepseek.com/{endpoint}" response = requests.get(url, params=params) if response.status_code == 200: return response.json() else: raise Exception(f"Failed to fetch data: {response.text}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值