zoukankan      html  css  js  c++  java
  • koa2-cookie-session

    node.jspath.extname方法使用
      由于该方法属于path模块,使用前需要引入path模块(var path= require(“path”) )
      接收参数:
      p path 路径

    	path.extname('index.html')
    	// returns
    	'.html'
    	path.extname('index.')
    	// returns
    	'.'
    	path.extname('index')
    	// returns
    	空'
    

      

    node.js中的path.join方法使用说明
      方法说明:
      将多个参数组合成一个 path (详细请看例子)
      path.join([path1], [path2], [...])
      由于该方法属于path模块,使用前需要引入path模块(var path= require(“path”) )
      例子:

      

    	path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')
    	// returns
    	'/foo/bar/baz/asdf'
    	path.join('foo', {}, 'bar')
    	// throws exception
    	TypeError:参数路径。加入必须是字符串
    	TypeError: Arguments to path.join must be strings
    

    cookie

    koa2使用cookie 
    
    app.use(async(ctx)=>{
        if(ctx.url === '/index'){
    	//ctx.cookie.get() 读取上下文请求中的cookie
            ctx.cookies.set( // 写入cookie
                'cid',		    //cookie  的name
                'hello world',	    // cookie  的value
                {
                    domian:'localhost',//写入cookie所在的域名
                    path: '/index',    //写入cookie所在的路径  
                    maxAge: 20*60*1000, //cookie有效时间
                    httpOnly: false,//是否只用于http请求中获取
                    overwrite: false//是否允许重写
                }
            )
            ctx.body = 'cookie is ok'
        }else{
            ctx.body = 'hello world'
        }
    })
    

     session

    数据库存储方案

    session存放在MySQL数据库中
    需要用到中间件
    koa-session-minimal 适用于koa2 的session中间件,提供存储介质的读写接口 。
    koa-mysql-sessionkoa-session-minimal中间件提供MySQL数据库的session数据读写操作。
    将sessionId和对于的数据存到数据库
    将数据库的存储的sessionId存到页面的cookie
    根据cookiesessionId去获取对于的session信息

    首先需要natvicat中建一个数据库

    //配置存储session信息的mysql
    let store = new MysqlSession({
        user: 'root',//数据库用户名
        password: 'abc123',//用户密码
        database: 'koa_demo',//数据库名
        host: '127.0.0.1',//数据库默认地址
    
    });
    
    //cookie session  文件配置
    let cookie = {
        maxAge:'',//cookie过期时间
        path:'',//写入cookie所在的路径
        domain:'',//写入cookie所在的域名
        httpOnly: false,//是否只用于http请求
        overwrite:'',//是否准许重写
        secure: '',
        sameSite: '',
        signed: '',
    }
    
    //使用session 中间件,把上面配置的信息加载到session中间件中
    //session 必须是一个方法 所以app.use(session({}))
    app.use(session({
        key: 'SESSION_ID',//Name
        store: store,
        cookie: cookie
    }));
    
    app.use(async(ctx)=>{
        //访问/set  设置session
        if (ctx.url === '/set' ) {
            ctx.session = {
                user_id:Math.random().toString(36).substr(2),
                count: 0
            }
            ctx.body = ctx.session;
    	//否则 访问根目录  读取session数据
        }else if(ctx.url === '/'){
            //读取session 数据
            ctx.session.count = ctx.session.count + 1
            ctx.body = ctx.session
        }
    })
    

      

  • 相关阅读:
    【源码学习之spark core 1.6.1 standalone模式下的作业提交】
    【源码学习之spark streaming 1.6.1 】
    spark udf 初识初用
    spark 累加历史 + 统计全部 + 行转列
    spark 都用了哪些开源东东
    kafka 官方示例代码--消费者
    104. 二叉树的最大深度
    237. 删除链表中的节点
    Leetcode-167. Two Sum II
    Leetcode-215. Kth Largest Element in an Array
  • 原文地址:https://www.cnblogs.com/patriot/p/7449216.html
Copyright © 2011-2022 走看看