gpt4 book ai didi

node.js - 如何调试快速路由器

转载 作者:行者123 更新时间:2023-12-01 13:50:40 25 4
gpt4 key购买 nike

我正在尝试基于同一项目中的另一个模块将一个模块添加到一个巨大的项目中。

客户端以角度方式通过以下方式请求对象

$http.get('/foo/var');

服务器以这种方式注册路由:
var express = require('express');
module.exports = function(dependencies) {
var controller = require('./controller')(dependencies);
var router = express.Router();
router.get('/foo/var', controller.getInbox);

return router;
};

现在,这不起作用(请求 http://ulr:port/foo/var 返回 404)。我不是在问为什么这不起作用,而是为什么我不能像这样调试它:
var logger = dependencies('logger');//Works OK
var controller = require('./controller')(dependencies);

logger.debug('This string is being printed when registering this module');

var router = express.Router();
router.get('/foo/var', controller.getInbox);

logger.debug(router);//This makes the server crash
return router;

因此,每当我尝试记录路由器(无论是在我的功能失调的路由器中,还是在我的工作路由器中)时,服务器都会通过抛出此异常来拒绝启动
error: Fatal error: TypeError: Cannot read property 'method' of null
at Function.proto.handle (/path/to/my/project/node_modules/express/lib/router/index.js:114:33)
at router (/path/to/my/project/node_modules/express/lib/router/index.js:24:12)
at cb (/path/to/my/project/node_modules/winston/lib/winston/logger.js:206:7)
at done (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:132:19)
at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:32:16
at emit (/path/to/my/project/node_modules/winston/lib/winston/logger.js:196:7)
at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:122:13
at _each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:46:13)
at Object.async.each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:121:9)
at Logger.log (/path/to/my/project/node_modules/winston/lib/winston/logger.js:214:9)
error: TypeError: Cannot read property 'method' of null
at Function.proto.handle (/path/to/my/project/node_modules/express/lib/router/index.js:114:33)
at router (/path/to/my/project/node_modules/express/lib/router/index.js:24:12)
at cb (/path/to/my/project/node_modules/winston/lib/winston/logger.js:206:7)
at done (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:132:19)
at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:32:16
at emit (/path/to/my/project/node_modules/winston/lib/winston/logger.js:196:7)
at /path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:122:13
at _each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:46:13)
at Object.async.each (/path/to/my/project/node_modules/winston/node_modules/async/lib/async.js:121:9)
at Logger.log (/path/to/my/project/node_modules/winston/lib/winston/logger.js:214:9)

尽管很高兴知道我做错了什么,但我更愿意知道调试此问题的正确方法是什么

使用 @robertklep's indications 调试模块

我在调试中看到的是:
Wed, 12 Aug 2015 16:22:46 GMT express:router:route new /foo/var
Wed, 12 Aug 2015 16:22:46 GMT express:router:layer new /foo/var
Wed, 12 Aug 2015 16:22:46 GMT express:router:route get /foo/var

所以看起来这就是我想让我的东西听的端点,但它仍然不起作用,我想知道什么是层和路由的东西,以及为什么重复路由。

我找到了这个模块 https://www.npmjs.com/package/express-list-routes我正在测试它。

[编辑] Controller 代码
module.exports = function(dependencies) {
function getInbox() {
return [
{ date:'31/08/1986', from:"Moi même", to:"Myself", subject:"about"},
{ date:'01/01/1712', from:"Bill Murray", to:"Hulla Kaboolla", subject:"about"}
];
}

return {
getInbox: getInbox,
};
};

最佳答案

问题是由此引起的:

> typeof express.Router()
'function'

这使 Winston(您的记录器)认为 router是一个回调函数(它不是),并使用不正确的参数调用它,使 router抛出错误。

我不确定你为什么要登录 router ,但如果要调试Express,可以 read this .

关于node.js - 如何调试快速路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31963267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com