【问题标题】:API request through Docker & Nodejs fails but works perfectly when accessed directly using the Nodejs通过 Docker 和 Nodejs 的 API 请求失败,但在使用 Nodejs 直接访问时可以正常工作
【发布时间】:2022-02-28 21:29:49
【问题描述】:

我正在使用Nodejs and Docker 开发应用程序。在代码中,我需要向 GitLab API 发出请求以获取数据。当我通过 Docker 命令docker-compose exec web sh -c "project=GitLab type=New npm start" 运行应用程序时,我收到错误消息,因为它无法从 API 调用中获得响应,但是在使用直接命令 node index.js 运行时,相同的代码和 API 请求可以完美运行。

以下是我的代码:

./web/index.js:

const   express     =   require('express');
const   http        =   require("http");
const   bodyParser  =   require('body-parser');
const   app         =   express();
const   port        =   process.env.PORT || 9000;
const   gitlabDump  =   require("./controller/GitLabDump");

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: true }));

//Make NodeJS to Listen to a particular Port in Localhost
app.listen(port, function(){
    
    var project = process.env.project;
    var type    = process.env.type;

    if(project.trim() === "GitLab" && (type.trim() === "New" || type.trim() === "Update")){
        //If porject is GitLab then fetch the data from Gitlab
        console.log("Fetching GitLab Data.......");

        gitlabDump.gitlabDump(type, function(data){
            console.log("Completed Execution for GitLab")
            process.exit();
        })
    }
}

以下是我发出 API 请求的控制器代码: ./web/controller/GitLabDump.js

const request = require('request');

exports.gitlabDump = function(callback){
    var gitlabAPI = "https://gitlab.com/api/v4/projects/<project_id>/repository/tree?ref=<target_branch>&path=path/to/subdirectory";

    console.log("BEFORE \n")

    request(gitlabAPI, function(error, response, body) {
        console.log(JSON.parse(body)) 
        callback("Completed");
    })
}

以下是我的 DockerFile: ./docker.compose.yml

version: '3'

services:
  db:
    container_name: db
    image: mysql:5.7
    volumes:
      - "./data/mysql:/var/lib/mysql:rw"
    environment:
      MYSQL_DATABASE: myDatabase
      MYSQL_ROOT_PASSWORD: myPassword
      MYSQL_PASSWORD: myPassword
      DATABASE_HOST: localhost
    restart: always

  web:
    container_name: web
    image: node:8
    volumes:
      - ./web:/usr/src/app
    working_dir: /usr/src/app
    depends_on:
      - db
    restart: on-failure
    command: "tail -f /dev/null"
    environment: ["project=${project}", "type=${type}"]

以下是我用来运行应用程序的命令:

docker-compose exec web sh -c "project=GitLab type=New npm start"

以下是我得到的错误:

Fetching GitLab Data.......
BEFORE 

undefined:1
undefined
^

由于console.log(JSON.parse(body)) 行而出现错误。因为 body 未定义,因为 API 调用不返回数据。

请注意:

  1. API URL 是正确的,我有正确的访问权限,因为在通过 Chrome、Postman 访问时,甚至在使用 node index.js 命令运行代码时,相同的 URL 都会为我提供数据。

  2. 我正在使用同一个应用程序进行除 GitLab 之外的其他 API 调用,它们运行良好。

有人可以帮我解决这里的问题吗?为什么 GitLab API 会失败?

【问题讨论】:

  • @Arjan 是的,我也在那里发帖希望得到一些回应,因为这个问题从昨天开始就在吞噬我的大脑。
  • 这能回答你的问题吗? Executing 'bash -c' in 'docker exec' command
  • @sytech 非常感谢您的回答。基于上述答案,我尝试了docker-compose exec -it web sh -c "project=GitLab type=New npm start",但它似乎对我不起作用。您能否告诉我需要修改哪些其他内容才能解决此问题?

标签: javascript node.js docker docker-compose gitlab


【解决方案1】:

在此处发布答案,因为它可能对将来的其他人有用:

最后,我找到了解决方案。问题不是因为docker 而发生,而是因为Nodejs 本身。如果我们 console.log 错误然后我们得到certificate has expired 消息。

解决方法是 request 是这样的:

request({
  url: gitlabAPI,
  agentOptions: {
    rejectUnauthorized: false
  }
}, function (error, response, body) {
  console.log(JSON.parse(response.body))
});

参考问题:Node.js request CERT_HAS_EXPIRED

【讨论】:

    猜你喜欢
    • 2014-03-15
    • 2022-01-24
    • 1970-01-01
    • 2019-11-11
    • 1970-01-01
    • 2016-07-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多