无法在Node JS中调用控制器

时间:2019-05-04 07:18:42

标签: node.js

我无法使用Node Js在控制器中调用该方法。我可以成功地为节点提供服务并连接到数据库,但是未调用控制器的方法。请在下面查看我的代码:

这是在 config.js

var env = process.env.NODE_ENV || 'development';
var config = require('./config.json');
var envConfig = config[env];
Object.keys(envConfig).forEach(key => process.env[key] = envConfig[key]);

这是 config.json

{
    "development" : {
        "PORT" : 3000,
        "MONGODB_URI" : "mongodb://localhost:27017/Mean-Client-Db"
    },
    "production" : {
        "PORT": 80,
        "MONGODB_URI" : "mongodb://xxxx/B_Name"
    }
}

这是 db.js

const mongoose = require('mongoose');

mongoose.connect(process.env.MONGODB_URI, (err) => {
    if(!err) {
        console.log('Connected to Mongo Database');
    } else {
        console.log('Error in connecting to Mongo Database: '
        + JSON.stringify(err, undefined, 2));
    }
});

require('./user.model');

这是 user.model.js

const mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
    fullName: {
        type: String
    },
    email: {
        type: String
    },
    password: {
        type: String
    },
    selfSecret: String
});

mongoose.model('User', userSchema);

这是 index.router.js

const express = require('express');
const router = express.Router();

const ctrlUser = require('../controllers/user.controller');

router.get("/register", ctrlUser.register);

module.exports = router;

这是 app.js

require('./config/config');
require('./models/db');

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const rtsIndex = require('./routes/index.router');

var app = express();

app.use(bodyParser.json);
app.use(cors());
app.use('/api', rtsIndex);

app.listen(process.env.PORT, () => console.log(`Server started at port : ${process.env.PORT}`));

这是 user.controller.js

module.exports.register = (req, res, next) => {
    console.log('Inside register function');
}

I am using the postman to access this url "http://localhost:3000/api/egister". I can serve the node right and connect to the database it's just I cannot console log out the message in my controller. I need your help on this. Thank you.

0 个答案:

没有答案