Hướng dẫn nodejs log line number - số dòng nhật ký nodejs

Tôi đã tìm thấy câu trả lời của Dmitry Druganov rất hay, nhưng tôi đã thử nó trên Windows 10 (với Node 8.9.4) và nó không hoạt động tốt. Đó là in toàn bộ con đường, một cái gì đó như:

Loading settings.json
   at fs.readdirSync.filter.forEach (D:\Users\Piyin\Projects\test\settings.js:21:13)
Server is running on http://localhost:3000 or http://127.0.0.1:3000
   at Server.app.listen (D:\Users\Piyin\Projects\test\index.js:67:11)

Vì vậy, tôi đã nói câu trả lời và thực hiện những cải tiến này (theo quan điểm của tôi):

  • Giả sử dòng quan trọng của dấu vết ngăn xếp là thứ ba (phần đầu tiên là từ Error và thứ hai là nơi bạn đặt tập lệnh này)
  • Xóa đường dẫn thư mục tập lệnh hiện tại (được đưa ra bởi __dirname, trong trường hợp của tôi là D:\Users\Piyin\Projects\test). Lưu ý: Để điều này hoạt động tốt, tập lệnh phải có trên JavaScript chính của dự án
  • Xóa bắt đầu at
  • Đặt thông tin tệp trước nhật ký thực tế
  • Định dạng thông tin là
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    0

Đây là:

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});

Và đây là đầu ra mới:

fs.readdirSync.filter.forEach at settings.js:21:13
 Loading settings.json
Server.app.listen at index.js:67:11
 Server is running on http://localhost:3000 or http://127.0.0.1:3000

Đây là mã được thu nhỏ bởi mã (240 byte):

['log','warn','error'].forEach(a=>{let b=console[a];console[a]=(...c)=>{try{throw new Error}catch(d){b.apply(console,[d.stack.split('\n')[2].trim().substring(3).replace(__dirname,'').replace(/\s\(./,' at ').replace(/\)/,''),'\n',...c])}}});

Giả sử bạn đang chạy Node.js trên một máy từ xa, Remote.example.com, mà bạn muốn có thể gỡ lỗi. Trên máy đó, bạn nên khởi động quá trình nút với trình kiểm tra chỉ nghe localhost (mặc định).Public • Published a year ago

  • Bây giờ, trên máy cục bộ của bạn từ nơi bạn muốn bắt đầu kết nối máy khách gỡ lỗi, bạn có thể thiết lập một đường hầm SSH:
  • Điều này bắt đầu một phiên đường hầm SSH trong đó kết nối với cổng 9221 trên máy cục bộ của bạn sẽ được chuyển tiếp đến cổng 9229 trên Remote.example.com. Bây giờ bạn có thể đính kèm một trình gỡ lỗi như Chrome Devtools hoặc Visual Studio Code vào localhost: 9221, có thể gỡ lỗi như thể ứng dụng Node.js đang chạy cục bộ.BETA
  • Trình gỡ lỗi kế thừa đã được không dùng từ Node.js 7.7.0. Vui lòng sử dụng
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    1 và thanh tra thay thế.
  • Khi bắt đầu với các chuyển đổi-Debug hoặc--Debug-Brk trong phiên bản 7 trở lên, Node.js lắng nghe các lệnh gỡ lỗi được xác định bởi giao thức gỡ lỗi V8 đã ngừng trên cổng TCP, theo mặc định
    ['log','warn','error'].forEach(a=>{let b=console[a];console[a]=(...c)=>{try{throw new Error}catch(d){b.apply(console,[d.stack.split('\n')[2].trim().substring(3).replace(__dirname,'').replace(/\s\(./,' at ').replace(/\)/,''),'\n',...c])}}});
    
    1. Bất kỳ máy khách gỡ lỗi nào nói về giao thức này có thể kết nối và gỡ lỗi quá trình chạy; Một vài cái phổ biến được liệt kê dưới đây.
  • Giao thức gỡ lỗi V8 không còn được duy trì hoặc ghi lại.

Bắt đầu ['log','warn','error'].forEach(a=>{let b=console[a];console[a]=(...c)=>{try{throw new Error}catch(d){b.apply(console,[d.stack.split('\n')[2].trim().substring(3).replace(__dirname,'').replace(/\s\(./,' at ').replace(/\)/,''),'\n',...c])}}}); 2 để bắt đầu tập lệnh của bạn dưới trình gỡ lỗi dòng lệnh tích hợp. Tập lệnh của bạn bắt đầu trong quy trình Node.js khác bắt đầu với tùy chọn ['log','warn','error'].forEach(a=>{let b=console[a];console[a]=(...c)=>{try{throw new Error}catch(d){b.apply(console,[d.stack.split('\n')[2].trim().substring(3).replace(__dirname,'').replace(/\s\(./,' at ').replace(/\)/,''),'\n',...c])}}}); 3 và quy trình Node.js ban đầu chạy tập lệnh ['log','warn','error'].forEach(a=>{let b=console[a];console[a]=(...c)=>{try{throw new Error}catch(d){b.apply(console,[d.stack.split('\n')[2].trim().substring(3).replace(__dirname,'').replace(/\s\(./,' at ').replace(/\)/,''),'\n',...c])}}}); 4 và kết nối với mục tiêu của bạn.

Gỡ lỗi ứng dụng Node.js của bạn với Chrome Devtools bằng cách sử dụng quy trình trung gian dịch giao thức Thanh tra được sử dụng trong crom cho giao thức trình gỡ lỗi V8 được sử dụng trong Node.js.

1.0.5 & nbsp; • & nbsp; public & nbsp; • & nbsp; xuất bản một năm trước

Readme

Khám phá Beta

const logger = require('logger-line-number')

logger.log('test')
//2021-02-03 13:28:30 test.js:3:8 [INFO] test

logger.error('error')
2021-02-03 13:28:30 test.js:3:8 [ERROR] error

logger.debug('debug')
//2021-02-03 13:28:30 test.js:3:8 [DEBUG] debug

logger.setLogType(logger.LOG_TYPES.NONE)  //禁用打印日志
logger.setLogType(logger.LOG_TYPES.ERROR)  //打印 错误 日志
logger.setLogType(logger.LOG_TYPES.NORMAL)  //打印 错误 消息 日志
logger.setLogType(logger.LOG_TYPES.DEBUG)  //打印 错误 消息 调试 日志

0 phụ thuộc

Hướng dẫn này sẽ giúp bạn bắt đầu gỡ lỗi ứng dụng và tập lệnh Node.js của bạn.

Khi bắt đầu với công tắc

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});
1, quy trình Node.js lắng nghe cho một máy khách gỡ lỗi. Theo mặc định, nó sẽ lắng nghe tại máy chủ và cổng 127.0.0.1:9229. Mỗi quá trình cũng được gán một UUID duy nhất.

Khách hàng của Thanh tra phải biết và chỉ định địa chỉ máy chủ, cổng và UUID để kết nối. Một URL đầy đủ sẽ trông giống như

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});
2.

Node.js cũng sẽ bắt đầu nghe tin nhắn gỡ lỗi nếu nhận được tín hiệu

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});
3. . Trong Node.js 8 trở lên, nó sẽ kích hoạt API của Thanh tra.


Vì trình gỡ lỗi có quyền truy cập đầy đủ vào môi trường thực thi Node.js, một tác nhân độc hại có thể kết nối với cổng này có thể thực thi mã tùy ý thay mặt cho quy trình Node.js. Điều quan trọng là phải hiểu ý nghĩa bảo mật của việc đưa cổng trình gỡ lỗi trên mạng công cộng và tư nhân.

Nếu trình gỡ lỗi bị ràng buộc với địa chỉ IP công khai hoặc 0,0.0.0, bất kỳ máy khách nào có thể đến địa chỉ IP của bạn sẽ có thể kết nối với trình gỡ lỗi mà không có bất kỳ hạn chế nào và sẽ có thể chạy mã tùy ý.

Theo mặc định

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});
5 liên kết với 127.0.0.1. Bạn cần cung cấp một địa chỉ IP công cộng hoặc 0,0.0.0, v.v., nếu bạn có ý định cho phép các kết nối bên ngoài với trình gỡ lỗi. Làm như vậy có thể đưa bạn đến một mối đe dọa an ninh có khả năng đáng kể. Chúng tôi khuyên bạn nên đảm bảo tường lửa thích hợp và điều khiển truy cập tại chỗ để ngăn ngừa phơi nhiễm bảo mật.

Xem phần về 'Kích hoạt các kịch bản gỡ lỗi từ xa' theo một số lời khuyên về cách cho phép khách hàng gỡ lỗi từ xa kết nối một cách an toàn.

Ngay cả khi bạn liên kết cổng Thanh tra với 127.0.0.1 (mặc định), bất kỳ ứng dụng nào chạy cục bộ trên máy của bạn sẽ có quyền truy cập không giới hạn. Điều này là do thiết kế để cho phép gỡ lỗi cục bộ có thể gắn kết thuận tiện.

Trình duyệt, websockets và chính sách đồng nghĩa

Các trang web mở trong trình duyệt web có thể thực hiện các yêu cầu của WebSocket và HTTP trong mô hình bảo mật trình duyệt. Kết nối HTTP ban đầu là cần thiết để có được ID phiên gỡ lỗi duy nhất. Chính sách có nguồn gốc cùng với các trang web không thể thực hiện kết nối HTTP này. Để bảo mật bổ sung chống lại các cuộc tấn công phục hồi DNS, Node.js xác minh rằng các tiêu đề 'máy chủ' cho kết nối chỉ định địa chỉ IP hoặc

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});
6 chính xác.

Các chính sách bảo mật này không cho phép kết nối với một máy chủ gỡ lỗi từ xa bằng cách chỉ định tên máy chủ. Bạn có thể làm việc xung quanh hạn chế này bằng cách chỉ định địa chỉ IP hoặc bằng cách sử dụng các đường hầm SSH như được mô tả dưới đây.

Một trình gỡ lỗi CLI tối thiểu có sẵn với

['log','warn','error'].forEach((methodName) => {
  const originalMethod = console[methodName];
  console[methodName] = (...args) => {
    try {
      throw new Error();
    } catch (error) {
      originalMethod.apply(
        console,
        [
          (
            error
            .stack // Grabs the stack trace
            .split('\n')[2] // Grabs third line
            .trim() // Removes spaces
            .substring(3) // Removes three first characters ("at ")
            .replace(__dirname, '') // Removes script folder path
            .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
            .replace(/\)/, '') // Removes last parentheses
          ),
          '\n',
          ...args
        ]
      );
    }
  };
});
7. Một số công cụ thương mại và nguồn mở cũng có thể kết nối với thanh tra Node.js.

  • Tùy chọn 1: Mở
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    8 trong trình duyệt dựa trên crom hoặc
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    9 ở cạnh. Nhấp vào nút Định cấu hình và đảm bảo máy chủ và cổng mục tiêu của bạn được liệt kê.
    : Open
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    8 in a Chromium-based browser or
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    9 in Edge. Click the Configure button and ensure your target host and port are listed.
  • Tùy chọn 2: Sao chép
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    0 từ đầu ra của
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    1 (xem ở trên) hoặc văn bản gợi ý và dán vào chrome.
    : Copy the
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    0 from the output of
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    1 (see above) or the --inspect hint text and paste into Chrome.

Lưu ý rằng Node.js và Chrome cần được chạy trên cùng một nền tảng.

  • Trong bảng gỡ lỗi, nhấp vào biểu tượng Cài đặt để mở
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    2. Chọn "Node.js" để thiết lập ban đầu.
  • Chọn "Debug> Bắt đầu gỡ lỗi" từ menu hoặc nhấn F5.
  • Hướng dẫn chi tiết.

JetBrains Webstorm và các IDEBRAINS IDE khác

  • Tạo một cấu hình gỡ lỗi Node.js mới và gỡ lỗi.
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    1 sẽ được sử dụng theo mặc định cho Node.js 7+. Để vô hiệu hóa bỏ chọn
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    4 trong sổ đăng ký IDE. Để tìm hiểu thêm về việc chạy và gỡ lỗi Node.js trong WebStorm và các IDE JetBrains khác, hãy xem WebStorm Online Help.
  • Thư viện để dễ dàng kết nối với các điểm cuối giao thức của Thanh tra.
  • Bắt đầu cấu hình gỡ lỗi Node.js từ chế độ xem
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    5 hoặc nhấn
    fs.readdirSync.filter.forEach at settings.js:21:13
     Loading settings.json
    Server.app.listen at index.js:67:11
     Server is running on http://localhost:3000 or http://127.0.0.1:3000
    
    6. Hướng dẫn chi tiết
  • Từ tệp .js, chọn "Debug dưới dạng ...> Chương trình nút" hoặc
  • Tạo cấu hình gỡ lỗi để đính kèm trình gỡ lỗi vào ứng dụng Runness.js (đã được bắt đầu bằng
    ['log','warn','error'].forEach((methodName) => {
      const originalMethod = console[methodName];
      console[methodName] = (...args) => {
        try {
          throw new Error();
        } catch (error) {
          originalMethod.apply(
            console,
            [
              (
                error
                .stack // Grabs the stack trace
                .split('\n')[2] // Grabs third line
                .trim() // Removes spaces
                .substring(3) // Removes three first characters ("at ")
                .replace(__dirname, '') // Removes script folder path
                .replace(/\s\(./, ' at ') // Removes first parentheses and replaces it with " at "
                .replace(/\)/, '') // Removes last parentheses
              ),
              '\n',
              ...args
            ]
          );
        }
      };
    });
    
    1).

Tùy chọn dòng lệnh

Bảng sau đây liệt kê tác động của các cờ thời gian chạy khác nhau đối với việc gỡ lỗi:

Lá cờNghĩa
--quan sát
  • Kích hoạt đại lý thanh tra
  • Nghe trên địa chỉ mặc định và cổng (127.0.0.1:9229)
--Inspect = [host: cổng]
  • Kích hoạt đại lý thanh tra
  • Liên kết với địa chỉ hoặc máy chủ lưu trữ (mặc định: 127.0.0.1)
  • Nghe trên cổng cổng (mặc định: 9229)
--Inspect-brk
  • Kích hoạt đại lý thanh tra
  • Nghe trên địa chỉ mặc định và cổng (127.0.0.1:9229)
  • Liên kết với địa chỉ hoặc máy chủ lưu trữ (mặc định: 127.0.0.1)
Nghe trên cổng cổng (mặc định: 9229)
  • Kích hoạt đại lý thanh tra
  • Liên kết với địa chỉ hoặc máy chủ lưu trữ (mặc định: 127.0.0.1)
  • Nghe trên cổng cổng (mặc định: 9229)
  • Liên kết với địa chỉ hoặc máy chủ lưu trữ (mặc định: 127.0.0.1)
Nghe trên cổng cổng (mặc định: 9229)
  • --Inspect-brk
Phá vỡ trước khi mã người dùng bắt đầu
  • --Inspect-brk
  • Nghe trên cổng cổng (mặc định: 9229)

--Inspect-brk

Phá vỡ trước khi mã người dùng bắt đầu

node --inspect server.js

--Inspect-brk = [host: port]

ssh -L 9221:localhost:9229 [email protected]

fs.readdirSync.filter.forEach at settings.js:21:13
 Loading settings.json
Server.app.listen at index.js:67:11
 Server is running on http://localhost:3000 or http://127.0.0.1:3000
8


Spawn con quy trình để chạy tập lệnh của người dùng dưới cờ --Inspect; và sử dụng quy trình chính để chạy trình gỡ lỗi CLI.

fs.readdirSync.filter.forEach at settings.js:21:13
 Loading settings.json
Server.app.listen at index.js:67:11
 Server is running on http://localhost:3000 or http://127.0.0.1:3000
9--debug or --debug-brk switches in version 7 and earlier, Node.js listens for debugging commands defined by the discontinued V8 Debugging Protocol on a TCP port, by default
['log','warn','error'].forEach(a=>{let b=console[a];console[a]=(...c)=>{try{throw new Error}catch(d){b.apply(console,[d.stack.split('\n')[2].trim().substring(3).replace(__dirname,'').replace(/\s\(./,' at ').replace(/\)/,''),'\n',...c])}}});
1. Any debugger client which speaks this protocol can connect to and debug the running process; a couple popular ones are listed below.

Chúng tôi khuyên bạn nên không bao giờ có trình gỡ lỗi lắng nghe địa chỉ IP công khai. Nếu bạn cần cho phép các kết nối gỡ lỗi từ xa, chúng tôi khuyên bạn nên sử dụng các đường hầm SSH. Chúng tôi chỉ cung cấp ví dụ sau đây cho mục đích minh họa. Vui lòng hiểu rủi ro bảo mật khi cho phép truy cập từ xa vào một dịch vụ đặc quyền trước khi tiến hành.

Giả sử bạn đang chạy Node.js trên một máy từ xa, Remote.example.com, mà bạn muốn có thể gỡ lỗi. Trên máy đó, bạn nên khởi động quá trình nút với trình kiểm tra chỉ nghe localhost (mặc định).

Bây giờ, trên máy cục bộ của bạn từ nơi bạn muốn bắt đầu kết nối máy khách gỡ lỗi, bạn có thể thiết lập một đường hầm SSH: