快速应用程序的Route.get()需要一个回调函数,但是出现[对象未定义]错误

时间:2017-10-23 04:28:08

标签: javascript node.js express docker routing

我有以下health.js文件..

var health = function(req, res, done) {};
health.prototype.endpoint = function(req, res, done) {
    let http_status = 200;
    console.log("--- Sending health endpoint status of %s", http_status);
    res.sendStatus(http_status);
    done();
};

module.exports = health;

routes.js

中调用了哪个
module.exports = function (app) {
    let index = require('./endpoints/helloworld'),
        health = require('./endpoints/health').endpoint,
        metadata = require('./endpoints/metadata');

    app.get('/', index.endpoint);
    app.get('/health', health);
    app.get('/metadata', metadata.endpoint);
};

但是当我尝试运行我的应用时,收到以下错误..

Attaching to myapp_app_1
app_1  | npm info it worked if it ends with ok
app_1  | npm info using npm@3.10.10
app_1  | npm info using node@v6.11.4
app_1  | npm info lifecycle my-app@1.0.0~prestart-local: my-app@1.0.0
app_1  | npm info lifecycle my-app@1.0.0~start-local: my-app@1.0.0
app_1  |
app_1  | > my-app@1.0.0 start-local /app
app_1  | > node ./src/index.js
app_1  |
app_1  | /app/node_modules/express/lib/router/route.js:202
app_1  |         throw new Error(msg);
app_1  |         ^
app_1  |
app_1  | Error: Route.get() requires a callback function but got a [object Undefined]
app_1  |     at Route.(anonymous function) [as get] (/app/node_modules/express/lib/router/route.js:202:15)
app_1  |     at EventEmitter.app.(anonymous function) [as get] (/app/node_modules/express/lib/application.js:482:19)
app_1  |     at module.exports (/app/src/routes.js:10:9)
app_1  |     at Object.<anonymous> (/app/src/index.js:12:20)
app_1  |     at Module._compile (module.js:570:32)
app_1  |     at Object.Module._extensions..js (module.js:579:10)
app_1  |     at Module.load (module.js:487:32)
app_1  |     at tryModuleLoad (module.js:446:12)
app_1  |     at Function.Module._load (module.js:438:3)
app_1  |     at Module.runMain (module.js:604:10)
app_1  |
app_1  | npm info lifecycle my-app@1.0.0~start-local: Failed to exec start-local script
app_1  | npm ERR! Linux 4.9.49-moby
app_1  | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start-local"
app_1  | npm ERR! node v6.11.4
app_1  | npm ERR! npm  v3.10.10
app_1  | npm ERR! code ELIFECYCLE
app_1  | npm ERR! my-app@1.0.0 start-local: `node ./src/index.js`
app_1  | npm ERR! Exit status 1
app_1  | npm ERR!
app_1  | npm ERR! Failed at the my-app@1.0.0 start-local script 'node ./src/index.js'.
app_1  | npm ERR! Make sure you have the latest version of node.js and npm installed.
app_1  | npm ERR! If you do, this is most likely a problem with the my-app package,
app_1  | npm ERR! not with npm itself.
app_1  | npm ERR! Tell the author that this fails on your system:
app_1  | npm ERR!     node ./src/index.js
app_1  | npm ERR! You can get information on how to open an issue for this project with:
app_1  | npm ERR!     npm bugs my-app
app_1  | npm ERR! Or if that isn't available, you can get their info via:
app_1  | npm ERR!     npm owner ls my-app
app_1  | npm ERR! There is likely additional logging output above.
app_1  | npm ERR! Linux 4.9.49-moby
app_1  | npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "start-local"
app_1  | npm ERR! node v6.11.4
app_1  | npm ERR! npm  v3.10.10
app_1  | npm ERR! path npm-debug.log.896050243
app_1  | npm ERR! code EACCES
app_1  | npm ERR! errno -13
app_1  | npm ERR! syscall open
app_1  |
app_1  | npm ERR! Error: EACCES: permission denied, open 'npm-debug.log.896050243'
app_1  | npm ERR!     at Error (native)
app_1  | npm ERR!  { Error: EACCES: permission denied, open 'npm-debug.log.896050243'
app_1  | npm ERR!     at Error (native)
app_1  | npm ERR!   errno: -13,
app_1  | npm ERR!   code: 'EACCES',
app_1  | npm ERR!   syscall: 'open',
app_1  | npm ERR!   path: 'npm-debug.log.896050243' }
app_1  | npm ERR!
app_1  | npm ERR! Please try running this command again as root/Administrator.
app_1  |
app_1  | npm ERR! Please include the following file with any support request:
app_1  | npm ERR!     /app/npm-debug.log

为什么我收到此错误?

3 个答案:

答案 0 :(得分:1)

我不清楚你要做的是什么,但我可以解释为什么你会看到这个错误。

以下是health.js的相关位:

var health = function(req, res, done) {};

health.prototype.endpoint = function(req, res, done) {
   ...
};

module.exports = health;

所以你有一个名为health的空函数,然后你已经为它添加了一些原型。除非你计划创建health的实例,否则在原型中添加一些东西有点奇怪,如下所示:

var myHealth = new health();
var endpoint = health.endpoint;

似乎不太可能是你的想法。

在您的routes.js中,您已获得此信息:

health = require('./endpoints/health').endpoint,

变量health将为undefined,因为导出的函数没有endpoint属性。您可以通过prototype进行修复来修复它,但我再也不认为它真的是您想要的:

health = require('./endpoints/health').prototype.endpoint,

如果需要将空health函数维护为导出的内容,那么您可以删除prototype中的单词health.js

health.endpoint = function(req, res, done) {
    ...
};

但是,如果您不需要那个空函数并且只想导出名为endpoint的内容,则可以将health.js更改为以下内容:

exports.endpoint = function(req, res, done) {
    ...
};

答案 1 :(得分:0)

max+1

尝试将健康视为 health = require('./endpoints/health').endpoint,

之类的模块

答案 2 :(得分:0)

您尚未发布整个错误。这样我们就可以找到错误的行号。但是如果回调提供给它的路由没有解析为函数或者未定义,则express通常会抛出此错误。 如果您为每条路线指定了正确的路径,则可以重新检查。