Это для факультетского проекта.Мы должны использовать модель MVC, поэтому мой вопрос заключается в том, как я могу вызвать мой user.controller.js на моем маршруте, когда кто-то переходит на localhost: 3000 / register.Когда я тестирую его с почтальоном, он ничего не делает, даже console.log, а просто сидит и делает запрос навсегда.Я, очевидно, делаю что-то очень неправильно.
Контроллер
const bcrypt = require("bcrypt");
const expressValidator = require("express-validator");
let User = require("../model/schemas/user.schema.js");
function registerUser(req, res) {
// Get inputs
const username = req.body.name;
const email = req.body.email;
const password = req.body.password;
const passwordConfirm = req.body.passwordConfirm;
// Validate inputs
req.checkBody("username", "É necessário inserir um nome de utilizador.").notEmpty();
req.checkBody("email", "É necessário inserir um endereço de email.").notEmpty();
req.checkBody("email", "É necessário inserir um endereço de email válido.").isEmail();
req.checkBody("password", "É necessário inserir uma password").notEmpty();
req.checkBody("passwordConfirm", "As passwords não coincidem.").equals(req.body.password);
let errors = req.validationErrors();
if(errors){
res.status(500).send(errors);
} else {
// If valide create new User
let newUser = new User({
username: username,
email: email,
password: password,
profilePic: "",
upVotes: 0,
downVotes: 0,
experience: 0
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err){
console.log(err);
}
newUser.password = hash;
newUser.save((err) => {
if(err){
console.log(err);
return;
} else {
req.flash("success", "Registo efetuado com successo!");
res.status(200).send();
}
})
})
})
}
}
module.exports = registerUser;
Маршрут
const express = require("express");
const router = express.Router();
const registerUser = require("../controller/user.controller.js");
router.post('/register', registerUser);
module.exports = router;
app.js
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const expressValidator = require("express-validator");
const flash = require('connect-flash');
const session = require('express-session');
const passport = require("passport");
const mongodb = require('mongodb');
const mongoose = require('mongoose');
const config = require("./config/database");
require('dotenv').config();
mongoose.connect(config.database);
let db = mongoose.connection;
db.once("open", () => {
console.log("Connected to MongoDB Atlas");
});
db.on("error", (err) => {
console.log(err);
});
const app = express();
//Body-parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Routes
const home = require('./routes/home.js');
const ask = require('./routes/ask.js');
const questionPage = require('./routes/question-page.js');
const forum = require('./routes/forum.js');
const ranking = require('./routes/ranking.js');
const profile = require('./routes/profile.js');
// Routing, this is the route where /register is at
app.use("/", home);
// PORT
const port = process.env.PORT;
app.listen(port, () => {
console.log(`App listening on port `, port)
});