HTTP协议原理+实践

3-9 Redirect

2018-12-06  本文已影响0人  伯纳乌的追风少年

redirect

通过url去访问资源,资源不在原来的位置,服务器要告诉浏览器,请求的资源在哪里,浏览器再重新请求
Location:新url
302:临时跳转(永远都在服务器端做跳转)
301:永久跳转(告诉浏览器下次访问直接在浏览器端做跳转,301使用要非常慎重,如果用户不清浏览器缓存会一直跳转)

const http=require('http');
const fs=require('fs')
const zlib=require('zlib')

http.createServer(function(request,response){
    const html=fs.readFileSync('test.html')
    if (request.url==='/') {
 
      response.writeHead(302,{
        'Location':'/new'
      })
      response.end('')  
    };
    if (request.url==='/new') {
      response.writeHead(200,{
        'Content-Type':'text/html',
        'Content-Encoding':'gzip',
        'X-Content-Type-Options':'nosniff'
      })
      response.end(zlib.gzipSync(html))
    };
}).listen(8888)
console.log('server listening on 8888')
上一篇 下一篇

猜你喜欢

热点阅读