0

I am using express-generator to generate the basic routes.

Here is what i have so far in routes

var express = require('express');
var router = express.Router();
var ctrl = require('../controller/index');
var validation = require("../validations/user");
/* GET home page. */
router.route('/').get(ctrl.index);
router.route('/registerUser').post(validation.registerUser,ctrl.registerUser);

It is working fine.

After i add the below routes

router.route('/loginUser').post(validation.loginUser,ctrl.loginUser);
module.exports = router;

i am getting error like

opt/lampp/htdocs/MedApp/node_modules/express/lib/router/route.js:171
        throw new Error(msg);
        ^

Error: Route.post() requires callback functions but got a [object Undefined]
    at /opt/lampp/htdocs/MedApp/node_modules/express/lib/router/route.js:171:15
    at Array.forEach (native)

How can i allow more routes in the routes.js file ?

Help pls

4

2 回答 2

1

看来您还没有在 index.js 中导出 ctrl.registerUser 函数。

确保你有这样的东西

在验证/user.js

exports.registerUser=function(req,res,next){...};

exports.loginUser=function(req,res,next){...};

在控制器/index.js

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

exports.loginUser=function(req,res){...};
于 2016-10-04T07:08:43.560 回答
0

该错误指出Route.post() requires a callback.

你有:

router.route('/loginUser').post(validation.loginUser,ctrl.loginUser);

像这样使用回调函数:

router.post('/loginUser', function(req, res) {
     //run your loginUser logic here
});

module.exports = router; //make this file available to other files
于 2016-10-04T07:17:39.220 回答