简体   繁体   中英

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. 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. The nginx also servers as proxy for the API.

The API is located in the express container. It's a node.js application running on port 3000.

Then I have a mongo-container which is for persistence.

Docker- Compose for tech. 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. So that the api isn't accessible via it's port but via routing (so it only works in https).

Also there is a redirect from 80 -> 443 because of https and chromes new policy regarding http and https sites.

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 :)).

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:

{
    "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

  2. Tried this guide using socat containers: https://codefresh.io/docker-tutorial/debug_node_in_docker/ - without success

  3. started the application in debug mode, connected to the container and debuged via cli - API is not accessible from the web-client

  4. Canlde light debugging, which is horrible to perform, when you have to rebuild after every 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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