首页 > 作文

HTTP协议之重定向之NodeJs版解析

更新时间:2023-04-03 11:02:50 阅读: 评论:0

http协议之重定向——nodejs版

一、 重定向

1、基本介绍

url 重定向,也称为 url 转发,是一种当实际资源,如单萨镇冰个页面、表单或者整个 web 应用被迁移到新的 url 下的时候,保持(原有)链接可用的技术。http 协议提供了一种特殊形式的响应—— http 重定向(http redirects)来执行此类操作。
在 http 协议中,重定向操作由服务器通过发送特殊的响应而触发。http 协议的重定向响应的状态码为 3xx 。在接收到重定向响应的时候,会采用该响应提供的新的 url ,并立即进行加载;大多数情况下,除了会有一小部分性能损失之外,重定向操作对于用户来说是不可见的。

重定向一般用于整站迁移。

2、 location

location 首部指定的是需要将页面重新定向至的地址。一般在响应码为3xx的响应中才会有意义、

3、如何使用重定向?

heads添加location字段

二、 临时重定向——302

1、基本介绍

有时候请求的资源无法从其标准地址访问,但是却可以从另外的地方访问。在这种情况下可以使用临时重定向。搜索引擎不会记录该新的、临时的链接。在创建、更新或者删除资源的时候,临时重定向也可以用于显示临时性的进度页面。

2、状态码:302

3、代码示例:

const http = require("http");const fs = require("fs");const host = "127.0.0.1";const port = 8080;const rver = http.createrver((requestmsg, respon) => {    if (requestmsg.url === "/img") {        console.log("/img")        //301谨慎使用,否则只有客户自动清除缓存        // respon.writehead(301,"permanent redirect",{//此段代码只执行一次        //     "location":"/yxc"        // })        respon.writehead(302,"temporary redirect",{            "location":"/yqx"        })        respon.end();        // respon.writehead(200, "ok", {"content-type": "text/html; chart=utf-8"});        // respon.end("图片请求");    } el if (requestmsg.url === "/yxc") {        console.log("/yxc")        requestmsg.on("data", (data) => {            console.log("data:" + data.tostring())        })        respon.theader("content-type", "image/jpeg")        const reads = fs.createreadstream("./imgs/yxc.jpg");        reads.pipe(respon);    } el if (requestmsg.url === "/yqx") {        console.log("/yqx")        requestmsg.on("data", (data) => {            console.log("data:" + data.tostring())        })        respon.theader("content-type", "image/jpeg")        const reads = fs.createreadstream("./imgs/yqx.jpg");        reads.pipe(respon);    } el {        respon.writehead(500, "invalid request", {"content-type": "text/html; chart=utf-8"});        respon.end("无效请求");    }})rver.listen(port, host, () => {    console.log(`rver starting at ${host}:${port}`)})

4、代码说明:
执行上述代码后,如下代码可执行多次:

 respon.writehead(302,"temporary redirect",{             "location":"/yqx"         })
中药丰胸方法

当更改location值时,可以重新定位。

三、永久重定向——301

1、基本介绍

这种重定向操作是永久性的。它表示原 url 不应再被使用,而应该优先选用新的 url。搜索引擎机器人会在遇到该状态码时触发更新操作,在其索引库中修改与该资源相关的 url。

2、状态码:301

3、代码示例:

const http = require("http");const fs = require("fs");const host = "127.0.0.1";const port = 8080;const rver = http.createrver((requestmsg, respon) => {    if (requestmsg.url === "/img") {        console.log("/img")        //301谨慎使用,否则只有客户自动清除缓存        respon.writehead(301,"permanent redirect",{//此段代码只执行一次            "location":"/yxc"        })        // respon.writehead(302,"temporary r唯利是图句式edirect",{        //     "location":"/yqx"        // })        respon.end();        // respon.writehead(200, "ok", {"content-type": "text/html; chart=utf-8"});        // respon.end("图片请求");    } el if (requestmsg.url === "/yxc") {        console.log("/yxc")        requestmsg.on("data", (data) => {            console.log("data:" + data.tostring())        })        respon.theader("content-type", "image/jpeg")        const reads = fs.createreadstream("./imgs/yxc.jpg");        reads.pipe(respon);    } el if (requestmsg.url === "/yqx") {        console.log("/yqx")        requestmsg.on("data", (data) => {            console.log("data:" + data.tostring())        })        respon.theader("content-type", "image/jpeg")        const reads = fs.createreadstream("./imgs/yqx.jpg");        reads.pipe(respon);    } el {        respon.writehead(5电影手足情00, "invalid request", {"content-type": "text/html; chart=utf-8"});        respon.end("无效请求");    }})rver.listen(port, host, () => {    console.log(`rver starting at ${host}:${port}`)})

4、代码说明:

执行永久重定向代码后,如下代码只执行一次:

 respon.writehead(302,"tem月光色porary redirect",{             "location":"/yxc"         })

当更改location值后,

 respon.writehead(302,"temporary redirect",{             "location":"/yys"         })

ip定位使用的一直是 “location”:”/yqx”,直到清理缓存后,才会使用 “location”:”/yys”。
5、注意事项

永久重定向301谨慎使用,一旦设定永久重定向,浏览器将一直触发新操作,除非用户清理缓存。如果中途改变了网站迁移地址,必须用户清除缓存后才能访问到新网站,导致用户体验非常不好。

四、面试点:重定向的三个状态301 302 304

301:永久重定向
302:临时重定向
304:协商缓存

本文发布于:2023-04-03 11:02:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/0c362384ddcfa0b0f08380aae794d301.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:HTTP协议之重定向之NodeJs版解析.doc

本文 PDF 下载地址:HTTP协议之重定向之NodeJs版解析.pdf

标签:重定向   代码   缓存   操作
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图