HTTP笔记

2018-08-14  本文已影响21人  xingkong_s

如果你的代码是主要处理数据库和数据之间的逻辑 那么你就算是个后端
如果你的代码主要处理交互(用户的点击事件 鼠标事件 用户的一些流程)那么您就是一个前端

什么是服务端和客户端

server服务端是一个机器
client客户端也是一个机器
如果我们在chorme中输入一个网址
它会发一个请求到server //发起请求 request
一般server使用80端口 来接收这个请求
server呢 就返回一个响应 //返回响应 response

//注意:
在mac 或者 linux 系统上 80 端口是不能被普通用户使用的
用了就会报错
端口号只要大于1024 就可以了
那我非要用呢
可以 前面加 sudo

想知道的详细点
curl -v
v //verbose 啰嗦

➜  http-demo curl -v http://localhost:9999/main.css
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9999 (#0)
> GET /main.css HTTP/1.1
> Host: localhost:9999
> User-Agent: curl/7.52.1
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Content-Type: text/html;charset=utf-8
< Date: Mon, 13 Aug 2018 02:48:26 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
< 
* Curl_http_done: called premature == 0
* Connection #0 to host localhost left intact
呜呜呜% 

链接过程

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9999 (#0)

发起请求

> GET /main.css HTTP/1.1
> Host: localhost:9999
> User-Agent: curl/7.52.1
> Accept: */*
> 

大于号 表示我们发起的请求
小于号 表示 我们得到的响应
User-Agent 你用什么上网
Accept: / 你给我什么样的东西 我都要
> //回车也是一句话

response.end('sldf')
返回一个什么

返回的数据后 有一个 %
为什么? 表示 文件结尾

请求 request

响应 response

js css html json xml image jsonp本质

text/javascript application/javascript
text/css
text/html
text/json
text/xml application/xml text/xml+html
image/png
image/jpeg
image/gif
text/javascript =>jsonp

你浏览器地址栏 输入地址 默认请求 html
link 标签 请求css
script 标签 请求js
json 怎么请求

let request = new XMLHttpRequest()
request.open('GET','/3.json')
request.onload  = function(){
   console.log(request.responseText)
}
request.send()

jsonp就是在json的基础上 加 函数名(json)

Content-Type: application/x-www-form-urlencoded

缓存

/style response.setHeader('Cache-Control','max-age=3600')
浏览器在得到 /style 响应之后 3600s之内 不会再请求服务器
如果你想抛弃之前的缓存 , 你只需要把 /style 改下
/style?v=1

response.setHeader('Etag','xxx')
if(resquest.headers('if-none-match')==='xxx'){
    response.statusCode = 304
    response.end()
}

如果你第二次请求服务器时,带的特征值不变 我就304跳转,返回空
所以cache 和 Etag的区别就是 有没有请求服务器

cookie

cookie用来标示用户, 如那些用户登录了

if(query.xxx='xxx'){
    //如果查询参数里的xxx的值为...
    response.setHeader('Set-Cookie','login=true')
}
let d = new Date(0) // 1970
let xyz = d.toGMTString()
response.setHeader('Set-Cookie',`login=true;Expries=${xyz}`)
//过期的时间 为 GMT 时间

如果只是用简单的标记 true或者false 来表示用户的登录
前端很容易伪造的

document.cookie= "login=true"
session

为了 既能 标示用户 又能 防止用户篡改 引入了session
session={} 不能放到 createServer里面
不然每次启动的时候 都会重新创建

反向代理

正向代理 代理客户端
方向代理 代理服务端

买房的人 找中介买房 找房源 中介就是正向代理
卖房的人 也找中介卖房 找买房的人 中介就是反向代理

一般用nginx 来做反向代理

baidu server               nginx                     baidu
google server                                        google

命令

which nginx // 查询nginx位置
nginx -t  //查询 nginx 配置
service nginx start
sudo nginx

搜索

nginx 配置 静态 目录
nginx 配置 server name
上一篇 下一篇

猜你喜欢

热点阅读