简体   繁体   English

如何将Visual Studio代码调试器附加到在nginx代理后面的docker机器上运行的Node.JS应用程序

[英]How To Attach Visual Studio Code Debugger to Node.JS Application running on docker machine behind nginx proxy

I'm trying to attach a debugger to a node.js application. 我正在尝试将调试器附加到node.js应用程序。 So far I had no luck and it always ended in this error message: 到目前为止,我没有运气,它始终在此错误消息中结束:

在此输入图像描述

Here is how I set up the system: 以下是我设置系统的方法:

Overall 总体

I have three containers running on different networks: 我有三个容器在不同的网络上运行:

I have a frontend container with client side javascript and html code, running on a nginx server. 我有一个前端容器,带有客户端javascript和html代码,在nginx服务器上运行。 The nginx also servers as proxy for the API. nginx还将服务器作为API的代理。

The API is located in the express container. API位于快递容器中。 It's a node.js application running on port 3000. 它是在端口3000上运行的node.js应用程序。

Then I have a mongo-container which is for persistence. 然后我有一个用于持久性的mongo容器。

Docker- Compose for tech. Docker-为科技创作。 details: 细节:

version: '3.0' # specify docker-compose version


# Define the services/ containers to be run
services:
 frontend:
  container_name: frontend
  build: frontend 
  ports:
  - '80:80'
  - '443:443'
  networks: 
  - front
 express: # name of the second service
  container_name: express
  build: express-server # specify the directory of the Dockerfile
  ports:
  - '9229:9229'
  networks:
  - front
  - backbone
 mongo: # name of the third service
  container_name: mongo
  image: mongo # specify image to build container from
  volumes:
  - "db:/data/db"
  networks: 
  - backbone 
networks: 
  front:
    driver: bridge
  backbone:
    driver: bridge
volumes:
  db:

Frontend 前端

As mentioned before the Frontend Container routes calls to the "/api" subdomain to the corresponding express- container. 如前所述,前端容器将“/ api”子域的调用路由到相应的快递容器。 So that the api isn't accessible via it's port but via routing (so it only works in https). 因此api不能通过它的端口访问,而是通过路由(因此它只能在https中工作)。

Also there is a redirect from 80 -> 443 because of https and chromes new policy regarding http and https sites. 还有一个重定向从80 - > 443,因为https和chromes关于http和https网站的新政策。

server {
        listen 80;
        server_name _;



        return 301 https://$host$request_uri;
    }

    server {
        ssl on;
        ssl_certificate /etc/nginx/certs/server.crt;
        ssl_certificate_key /etc/nginx/certs/server.key;

        listen *:443 ssl;
        server_name _; 
        root /var/www;
        index index.html;

        location /api/ {
            rewrite ^/api/?(.*) /$1 break;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://express:3000;
        } 
    } 

Express 表达

The app is a simple REST- API which works almost out of the box (thanks express :)). 该应用程序是一个简单的REST-API,几乎开箱即用(感谢表达:))。

However one call runs into an exception and I want to debug it properly so I tried different approaches to attach a debugger. 然而,一个调用遇到异常,我想正确调试它,所以我尝试了不同的方法来附加调试器。

Debugger Configuration 调试器配置

I configured my debugger in visual studio code like this: 我在Visual Studio代码中配置了我的调试器,如下所示:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "attach",
            "name": "Attach to Remote",
            "address": "<docker-machine-ip>",
            "port": 9229,
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/usr/src/app"
        },
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/express-server\\app.js"
        }
    ]
}

What I've done so far 到目前为止我做了什么

  1. Opened direct access to the api and removed the routing via nginx, then opened the debug port on the express container and tried to debug: got a timeout and Error: connect ECONNREFUSED 打开直接访问api并通过nginx删除路由,然后打开快速容器上的调试端口并尝试调试:得到超时并且错误:连接ECONNREFUSED

  2. Tried this guide using socat containers: https://codefresh.io/docker-tutorial/debug_node_in_docker/ - without success 使用socat容器尝试了本指南: https ://codefresh.io/docker-tutorial/debug_node_in_docker/ - 没有成功

  3. started the application in debug mode, connected to the container and debuged via cli - API is not accessible from the web-client 在调试模式下启动应用程序,连接到容器并通过cli进行调试 - 无法从Web客户端访问API

  4. Canlde light debugging, which is horrible to perform, when you have to rebuild after every console.log() Canlde轻量级调试,当你必须在每个console.log()之后重建时执行起来很糟糕

If you need more information comment it, I would really appreciate a hint or maybe a tutorial. 如果您需要更多信息评论,我真的很感激提示或者教程。

Maybe I'm missing something 也许我错过了什么

Never mind I solved this. 没关系我解决了这个问题。

I had to change how I started the app (via node inspect instead of npm start) 我不得不改变我启动应用程序的方式(通过节点检查而不是npm start)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM