账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    request如何继续使用上一次请求返回的cookie?
    44
    0
    客户端
    import request from 'request-promise'
    
    
    request('http://localhost:8090/').then(res => {
        console.log(res)
    })
    
    request('http://localhost:8090/').then(res => {
        console.log(res)
    })
    
    request('http://localhost:8090/').then(res => {
        console.log(res)
    })
    服务端
    const Koa = require('koa')
    const route = require('koa-route')
    const bodyParser = require('koa-bodyparser')
    
    const app = new Koa()
    
    app.use(bodyParser({
        enableTypes: ['json', 'form', 'text']
    }))
    
    const main = ctx => {
        ctx.cookies.set('mycookie', 'hello-value');
        console.log(ctx.cookies.get('mycookie'))
        // 奇怪,这里为什么输出 undefined 呢?
        
        ctx.response.type = 'html'
        ctx.response.body = '<a href="/">main Page</a>'
    }
    
    app.use(route.get('/', main))
    app.listen(8090, '0.0.0.0', () => {
        console.log('Example app listening on http://localhost:8090/')
    })
    问题

    这里明明设置了cookie,但是输出 undefined ? 用浏览器就能正常的输出 hello-value

       ctx.cookies.set('mycookie', 'hello-value');
       console.log(ctx.cookies.get('mycookie'))
       console.log(ctx.cookies.get('mycookie')
    2
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 熔岩溪水 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部