web服务端重定向


#服务器重定向常见某些网站引导登陆页面(例如:淘宝点击购物车会跳转到登陆页面)!

  服务端的重定向功能主要由响应头的302 状态码来实现

 

  用nodejs,写的服务端重定向

//1.导入模块
const http = require('http');

const fs = require('fs');

const path = require('path');

//2.创建服务器
let server = http.createServer((req,res)=>{
console.log(req.url);
//请求路径
let urlPath = req.url;
//请求方法
let method = req.method;

if(req.url === '/'){
//302表示重定向
res.writeHead(302, {
'Location': 'login' //键值对,键表示客户端浏览器将进行重定向 值:表示客户端浏览器重定向的请求
//add other headers here...
});
res.end();
}
//登陆页
if(req.url === '/login'){
fs.readFile(path.join(__dirname,'login.html'),function(err,data){
if(err){
throw err;
}
res.end(data);
})
}
});


//3.开启服务器
server.listen(3000, ()=> {
console.log('服务器启动成功');
});

 

github: https://github.com/frjcxy 相互学习学习


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM