2017-10-18 57 views
1

我对请求模块使用了node.JS。节点请求如何处理会话

我的问题是,我需要在每个请求上对用户进行身份验证,因为会话在.then((response) => {})块之外被销毁。

如何将创建的会话保存在类中供以后使用?

我尝试了一切都没有成功。

下面是一个不工作代码片断

login() { 
const getLoginUrl = 'https://www.demourl.com/' 
const postLoginUrl = 'https://www.demourl.com/account/login/' 

rp({ 
     url: getLoginUrl, 
     jar: this.cookieJar, 
     method: 'GET' 
    }) 
    .then((body) => { 
     var csrftoken = this.cookieJar.getCookies(getLoginUrl)[1].toString().split('=')[1].split(';')[0]; 

     var args = { 
      url: postLoginUrl, 
      json: true, 
      method: 'POST', 
      data: { 
       username: this.username, 
       password: this.password 
      }, 
      headers: { 
       'method': 'POST', 
       'path': '/account/login/', 
       'cookie': 'csrftoken=' + csrftoken, 
      }, 
      jar: this.cookieJar, 
      resolveWithFullResponse: true 
     } 

     rp(args) 
      .then((response) => { 
       //Here is a valid session 
       //But how can I use this session in different functions? 
       console.log('Post demourl.com/account/login success'); 
      }) 
      .catch((error) => { 
       console.log('Post demourl.com/account/login error: ', error); 
      }); 
    }) 
    .catch((error) => { 
     console.log('Get demourl.com error: ', error); 
     }); 
} 
+1

如果您正在使用:

this.cookieJar = request.jar(); 

如果你只在你的应用程序中使用一个单一的cookieJar,你也可以通过设置选项jartrue使用要求的全球饼干罐express.js,请尝试使用中间件https://expressjs.com/zh/guide/using-middleware.html。如果没有,你可以有一个没有任何路径的中间件函数,它可以执行任何请求 –

回答

1

你应该使用这个功能作为一个中间件,然后附上你想在连接到您的REQ

尝试在你的主脚本什么都做

'use strict' 
 

 
const express = require('express'); 
 
const login = require('./login'); 
 
const app = express() 
 

 

 
app.use(login);// use this if you want all your routes to check login or put it in a specific route 
 

 
app.get('/', (req,res)=>{ 
 
//this route is only for loged in users 
 
}); 
 

 
const server = http.createServer(app).listen(process.env.PORT); 
 

 

 
module.exports = app;

,并在您登录脚本

const login = (req, res, next) => { 
 
    const getLoginUrl = 'https://www.demourl.com/' 
 
    const postLoginUrl = 'https://www.demourl.com/account/login/' 
 

 
    rp({url: getLoginUrl, jar: this.cookieJar, method: 'GET'}) 
 
     .then((body) => { 
 
      var csrftoken = this.cookieJar.getCookies(getLoginUrl)[1].toString().split('=')[1].split(';')[0]; 
 

 
      var args = { 
 
       url: postLoginUrl, 
 
       json: true, 
 
       method: 'POST', 
 
       data: { 
 
        username: this.username, 
 
        password: this.password 
 
       }, 
 
       headers: { 
 
        'method': 'POST', 
 
        'path': '/account/login/', 
 
        'cookie': 'csrftoken=' + csrftoken, 
 
       }, 
 
       jar: this.cookieJar, 
 
       resolveWithFullResponse: true 
 
      } 
 

 
      rp(args) 
 
       .then((response) => { 
 
        res.loginResponse = response; // save the response for later use 
 

 
        console.log('Post demourl.com/account/login success'); 
 
        next(); 
 
       }) 
 
       .catch((error) => { 
 
        console.log('Post demourl.com/account/login error: ', error); 
 
        return res.send(error) //send the error 
 
       }); 
 
     }) 
 
     .catch((error) => { 
 
      console.log('Get demourl.com error: ', error); 
 
      return res.send(error) //send the error 
 
     }); 
 
} 
 

 
module.exports = login

+0

如果没有中间件,那么没有办法使用这个函数吗? – Phil795

1

我从来没有看到被定义this.cookieJar。确保它的地方初始化:

// Either by setting it as the default 
const request = require('request').defaults({jar: true}); 

// Or by setting it on each request 
request('www.example.com', { jar: true }); 
+0

这种方式适用于我,通常我只在默认情况下将其定义为'var request = request.defaults({jar:true})' – Lai32290