内容简介:服务端设置CORS 只允许127.0.0.1:8888进行跨域请求就可以这样设置我们前端如果在header里携带了token,那么后台则需要做一下类似的配置:public(http经过的任何地方都可以进行缓存)
服务端设置CORS 只允许127.0.0.1:8888进行跨域请求就可以这样设置
response.writeHead(200,{
'Access-Control-Allow-Origin':'http://127.0.0.1:8888'
})
复制代码
CORS的预请求
我们前端如果在header里携带了token,那么后台则需要做一下类似的配置:
response.writeHead(200,{
'Access-Control-Allow-Origin':'http://127.0.0.1:8888',//允许请求的网址
'Access-Control-Allow-Headers':'Token',//允许携带的字段名称
'Access-Control-Allow-Methods':'POST,PUT,DELETE',//允许的方法类型
'Access-Control-Max-Age':'10000' //一万秒内让浏览器不再发起OPTION预请求,直接发起正式请求
})
复制代码
缓存头(Cache-Control)含义和使用
Cache-Control的可缓存性:
public(http经过的任何地方都可以进行缓存)
private (发起请求的服务器才可以进行缓存)
no-cache(都不可以进行缓存)
到期:
max-age=seconds(缓存多少秒后过期)
s-maxage=seconds(它能代替 max-age=seconds
,但是只有在代理服务器里才会生效,也就是说在浏览器端最终还是会读取 max-age=seconds
)
服务端如何设置Cache-Control
response.writeHead(200,{
'Cache-Control':'max-age=200'
})
复制代码
no-cache,Last-Modified,Etag
服务端如何设置no-cache
response.writeHead(200,{
'Cache-Control':'max-age=200000, no-cache',
'Last-Modified':'123',//随便写
'Etag':'777'//随便写
})
复制代码
设置no-cache后那个文件始终都会去服务器请求,不会读取本地的资源,max-age会失效 设置'Last-Modified'和'Etag'后浏览器在下次请求时会携带if-Modified-Since:123和if-Node-Match:777去验证资源是否被更改
浏览器就可以取得etag的值返回304告诉浏览器资源没有变更,直接读取本地就可以了
if(request.headers['if-none-match'] === '777'){
response.writeHead(304,{
'Cache-Control':'max-age=200000, no-cache',
'Last-Modified':'123',
'Etag':'777'
})
response.end('')
}else{
response.writeHead(200,{
'Cache-Control':'max-age=200000, no-cache',
'Last-Modified':'123',//随便写
'Etag':'777'//随便写
})
}
复制代码
如果服务端设置了no-store则会忽略header里面任何与缓存相关的参数,每次还是会去服务器请求资源
Cookie
服务端设置Cookie
response.writeHead(200,{
'Cache-Control':'max-age=200000,
'Set-Cookie':['id=123;max-age=2','abc=456;domain=test.com']
})
复制代码
以上所述就是小编给大家介绍的《自学NodeJS设置Header》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
An Introduction to Genetic Algorithms
Melanie Mitchell / MIT Press / 1998-2-6 / USD 45.00
Genetic algorithms have been used in science and engineering as adaptive algorithms for solving practical problems and as computational models of natural evolutionary systems. This brief, accessible i......一起来看看 《An Introduction to Genetic Algorithms》 这本书的介绍吧!