【问题标题】:how do I incorporate my node.js file into a postman request?如何将我的 node.js 文件合并到邮递员请求中?
【发布时间】:2017-12-01 02:52:17
【问题描述】:

我目前正在尝试获取一个为 aws cognito 身份验证生成两个令牌的脚本,并在我的邮递员测试中使用它。这是我正在尝试集成的代码...

const AWS = require('aws-sdk');
const CognitoSDK = require('amazon-cognito-identity-js-node');

var authenticationData = {
  Username: 'username', 
  Password: 'password' 
};
var authenticationDetails = new CognitoSDK.AuthenticationDetails(authenticationData);
var poolData = {
  UserPoolId: 'aws_region',
  ClientId: 'aws_user_client_id'
};
var userPool = new CognitoSDK.CognitoUserPool(poolData);
var userData = {
  Username: 'username', 
  Pool: userPool
};
var cognitoUser = new CognitoSDK.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess: function (result) {
      console.log('access token + ' + result.getAccessToken().getJwtToken());
      /*Use the idToken for Logins Map when Federating User Pools with Cognito Identity or when passing through an Authorization Header to an API Gateway Authorizer*/
      console.log('idtoken + ' + result.idToken.jwtToken);
  },
  onFailure: function (err) {
      console.log(`Error: ${err}`);
  },
});

我可以从命令行运行此文件并返回 2 个令牌,但是当我尝试通过将先前的代码复制并粘贴到预请求脚本中或将其保存为全局变量并使用将其合并到邮递员中时eval 函数,我在 Postman 中收到以下错误:

评估预请求脚本时出错:错误:找不到模块“aws-sdk”

我错过了什么?

【问题讨论】:

    标签: javascript node.js postman amazon-cognito


    【解决方案1】:

    预请求脚本在发送请求之前运行,仅此而已。您正在尝试导入一个包,但 Postman 不知道那是什么,没有 npm 本身可以导入这些外部 JS 文件。有一种解决方法,将您的外部 (aws-sdk) 脚本作为环境变量,然后运行 ​​

    eval(postman.getGlobalVariable('your_aws_sdk_code'))
    

    AFAIK 这是最简单的方法,它是同步的(我相信它比其他方法更适合您的情况)。 Postman 仍在努力整合一种更简单的方法,您可以在他们的github 上阅读更多相关信息。

    【讨论】:

    • 话题复兴。 @Z.Baker 简短的问题。我知道如何在 pm 全局变量中设置简单的函数,然后使用 eval 技巧从测试部分调用它们。但是,我不知道如何在 1 个简单的全局变量中导入复杂的项目(如 aws_sdk)......你能分享你的过程吗?对我来说,它应该像在全局变量中复制 js 代码一样简单......非常感谢您的任何反馈。 Bregards, MG。
    猜你喜欢
    • 2018-10-22
    • 2021-05-12
    • 1970-01-01
    • 2018-04-12
    • 2020-03-11
    • 2021-03-08
    • 2019-05-12
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多