企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 概念 当客户端(浏览器)向服务器发送一个URL请求后,但是资源并不在当前请求的服务器上,此时服务器会告诉浏览器,资源在另外一个URL地址上,此时浏览器会重新发送请求到新的资源地址。 # 使用 ## 301 永久跳转。跳转URL会缓存到用户浏览器,除非用户清理浏览器缓存,服务器设置无法再影响旧URL的跳转。 <br> ![](https://box.kancloud.cn/00bb7477c8a8ee07c8590cba081460c4_1067x146.png) <br> ~~~ const http = require('http') http.createServer(function (req, res) { console.log(`requeset come ${req.url}`) if (req.url === '/') { res.writeHead(301, { 'location': '/new' }) res.end() } if (req.url === '/new') { res.writeHead(200, { 'Content-type': 'text/html' }) res.end('<div>content</div>') } }).listen(8888) // 访问 http://localhost:8888/ 服务器直接打印 // requeset come /new ~~~ <br> ## 302 临时跳转。每次请求仍然需要经过服务端指定跳转地址。 <br> ![](https://box.kancloud.cn/2c18def9e695b93bf159ffa3c4142f18_1049x150.png) <br> ~~~ const http = require('http') http.createServer(function (req, res) { console.log(`requeset come ${req.url}`) // 临时跳转 if (req.url === '/') { res.writeHead(302, { 'location': '/new2' }) res.end() } if (req.url === '/new2') { res.writeHead(200, { 'Content-type': 'text/html' }) res.end('<div>content</div>') } }).listen(8888) console.log('server listen on 8888') ~~~ # 参考资料 [http之Redirect](https://blog.csdn.net/grapelove01/article/details/82810360)