调试方法
- 使用
console.log()
输出变量的值来观察程序执行过程中的数据变化
app.use(async (ctx) => {
console.log('Request received:', ctx.url);
ctx.body = 'Hello World';
});
- 使用
debug
模块来输出调试信息
const debug = require('debug')('koa:server');
app.use(async (ctx) => {
debug('Request received:', ctx.url);
ctx.body = 'Hello World';
});
- 使用Chrome DevTools来调试Koa应用程序
在启动应用程序时加上--inspect
参数,然后打开Chrome浏览器,访问chrome://inspect/#devices,点击”Configure…”按钮,添加本地网络连接。接着点击”Inspect”按钮即可在Chrome DevTools中调试应用程序。
node --inspect server.js