TypeError:app.route不是函数

时间:2017-09-22 17:07:42

标签: node.js

我是React / Express / Node世界的新手,我正在研究一个允许用户创建登录和创建任务的API的应用程序。

我正在尝试将两者放在同一个应用程序中,但是当我为任务列表调用GET时,我收到了这个错误:

TypeError: app.route is not a function
    at module.exports (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/server/routes/schedule.js:6:7)
    at Layer.handle [as handle_request] (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:317:13)
    at /home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:335:12)
    at next (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:275:10)
    at initialize (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/passport/lib/middleware/initialize.js:53:5)
    at Layer.handle [as handle_request] (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:317:13)
    at /home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:335:12)
    at next (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:275:10)
    at urlencodedParser (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/body-parser/lib/types/urlencoded.js:91:7)
    at Layer.handle [as handle_request] (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:317:13)
    at /home/slauriano/workspace/authentication-in-react-apps/part-2-json-web-token/node_modules/express/lib/router/index.js:284:7

这是路径文件夹中todoList.js的代码片段:

'use strict';
module.exports = function(app) {
var todoList = require('../controllers/todoListController');

// todoList Routes
app.route('/tasks')
    .get(todoList.list_all_tasks)
    .post(todoList.create_a_task);


app.route('/tasks/:taskId')
    .get(todoList.read_a_task)
    .put(todoList.update_a_task)
    .delete(todoList.delete_a_task);
};

这是我错过的任何其他内容?这是身份验证应用程序部分在同一路径文件夹中正常工作的片段:

const express = require('express');


const router = new express.Router();

router.get('/dashboard', (req, res) => {
  res.status(200).json({
    message: "You're authorized to see this secret message."
  });
});


module.exports = router;

我会感激任何帮助;)

这是我的存储库https://github.com/slaurianodev/agenda-app。随意克隆并帮助解决这个问题。

韩国社交协会

塞尔吉奥

1 个答案:

答案 0 :(得分:0)

您是否使用app参数调用todoList.js?当我使用另一个文件作为路由文件时,我通常会这样做:

var express = require('express');
var app = express();
var routes = require('<path to file>/todoList.js');
routes(app);

这样您就可以将应用程序传递给处理路由的文件。希望这会有所帮助。