Node.js学习(9)会话控制

cookie

DEMO:

let express = require('express')
//获取cookie需要用到第三方cookie-parser插件
let cookieParser = require('cookie-parser')

let app = express()

app.use(cookieParser())

app.get('/', (request, response) => {
    response.send('ok')
})

app.get('/setcookie', (request, response) => {
    //设置会话cookie
    response.cookie('demo1', '123')
    //设置持久化cookie(30秒过期)
    response.cookie('demo2', '456', {maxAge: 1000 * 30})
    response.send('ok')
})

app.get('/getcookie', (request, response) => {
    //获取cookie
    console.log(request.cookies)
    response.send('ok')
})

app.get('/delcookie', (request, response) => {
    //删除cookie 方法1
    response.cookie('demo1', '456', {maxAge: 0})
    //调用删除api 方法2
    response.clearCookie('demo2')
    response.send('ok')
})

app.listen(3000, (err) => {
    if (!err) {
        console.log('服务器启动成功')
    } else {
        console.log(err)
    }
})

session

express中操作cookie、session教程

http://www.884358.com/md/nodejs/04#/

发表评论

邮箱地址不会被公开。 必填项已用*标注