3-9 Redirect

作者: 伯纳乌的追风少年 | 来源:发表于2018-12-06 10:15 被阅读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')

相关文章

网友评论

    本文标题:3-9 Redirect

    本文链接:https://www.haomeiwen.com/subject/zfpfcqtx.html