代码之家  ›  专栏  ›  技术社区  ›  Alan B

谷歌云平台节点。js库

  •  3
  • Alan B  · 技术社区  · 8 年前

    我正在创建一个谷歌功能。然而,当我尝试部署到Google云平台时,我遇到了这个错误

    错误:(gcloud.beta.functions.deploy)操作错误:代码=3,消息=函数加载错误:文件索引中的代码。无法加载js。 您是否在软件包中列出了所有必需的模块。json依赖关系? 详细堆栈跟踪:错误:找不到模块“request”

    如何在谷歌云平台上传/安装“请求”库?

    代码段

    'use strict';
    const https = require('https');
    const host = 'https://www.example.com';
    const clientId = 'qpopMIGtVdeIdVk3oEtr2LGbn8vTeTWz';
    const clientSecret = 'eUnsWQ8y3AuiFHJu';
    const grant_type = 'client_credentials';
    const resource = 'b.microsoft.com/4fa4b4a7-d34f-49af-8781-c8b39f0cf770';
    const request = require("request");
    
    
    exports.oauthtoken = (req, res) => {
    
      // Call the Apigee API
      callGetOAuthToken().then((output) => {
        // Return the results from the APigee  to DialogFlow
        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify({ 'speech': output, 'displayText': output }));
      }).catch((error) => {     
            // If there is an error let the user know
        res.setHeader('Content-Type', 'application/json');
        res.send(JSON.stringify({ 'speech': error, 'displayText': error }));
      });
    };
    function callGetOAuthToken () {
      return new Promise((resolve, reject) => {
    
        let path = '/customers/v1/accesstoken';
    
        var authHeader =  Buffer.from(clientId + ':' + clientSecret).toString('base64');
        var post_options = {
                              url: host + path,
                              method: 'POST',
                              headers: 
                              {
                                'Content-Type': 'application/x-www-form-urlencoded',
                                'Authorization': 'Basic ' + authHeader,
                                'grant_type':grant_type
                              }
                            };
    
        // Make the HTTP request to get the weather
        request(post_options, function(err, res, body) {
            let output = JSON.parse(body);
            console.log(output);
            resolve(output);
          });   
      });
    }
    

    -艾伦-

    1 回复  |  直到 8 年前
        1
  •  3
  •   ansibly    8 年前

    阅读有关依赖关系的Google云文档: https://cloud.google.com/functions/docs/writing/dependencies

    将“请求”模块列为包中的依赖项。json文件(如果使用gcloud CLI)。

    或者,在包含云功能的文件夹中运行“npm install--save request”,并将预安装的依赖项作为ZIP文件的一部分上载。

    推荐文章