zoukankan      html  css  js  c++  java
  • 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 相互学习学习

    见习搬运工
  • 相关阅读:
    微信公众号项目部署
    数据库存入年月日时分秒类型时间问题
    路径问题
    常用DOS命令
    解决Maven下载慢的问题
    害人不浅的数学翻译
    Webpack4 踩坑记
    React 踩坑记
    what's the problem of Object oriented programming
    为什么JVM调优?
  • 原文地址:https://www.cnblogs.com/mound/p/10519402.html
Copyright © 2011-2022 走看看