首页 > 解决方案 > 如何解决 NodeJS 控制器不工作

问题描述

你好,我能得到一些帮助吗,我正在尝试在 NodeJS 上使用 MVC,所以我创建了一个控制器并将其导出到我的路由中,效果很好,但是当我尝试通过邮递员访问路由时,我得到了404没有找到,我可以就我可能做错的事情获得一些帮助。

这是我的身份验证控制器,只有注册想要完成这项工作,然后添加其余部分

import { userModel } from "../../Models/Users/Users";
import Bcrypt from "bcrypt";
import Formidable from "formidable";
import nodemailer from "nodemailer";
import dotenv from "dotenv";
dotenv.config();

class userAuth {
  SignUp(request, response) {
    const form = new Formidable.IncomingForm();

    try {
      form.parse(request, async (error, fields, files) => {
        const {
          username,
          firstName,
          lastName,
          email,
          password,
          verifiedPassword,
        } = fields;

        if (
          !username ||
          !firstName ||
          !lastName ||
          !email ||
          !password ||
          !verifiedPassword
        ) {
          return response
            .status(400)
            .json({ msg: "All fields have to be entered" });
        }

        if (password.length < 6) {
          return response
            .status(400)
            .json({ msg: "Password has to be at least 6 characters" });
        }

        if (password !== verifiedPassword) {
          return response.status(400).json({ msg: "Password have to match" });
        }

        const isExistingUserName = await userModel.findOne({
          username: username,
        });

        if (isExistingUserName) {
          return response
            .status(400)
            .json({ msg: "Account with this username already exist" });
        }

        const isExistingEmail = await userModel.findOne({ email: email });

        if (isExistingEmail) {
          return response
            .status(400)
            .json({ msg: "Account with this email already exist" });
        }

        const salt = await Bcrypt.genSalt(15);
        const hashedPassword = await Bcrypt.hash(password, salt);
        const newUser = new userModel({
          username,
          firstName,
          lastName,
          email,
          password: hashedPassword,
        });

        const savedUser = await newUser.save();

        const transporter = nodemailer.createTransport({
          service: "SendinBlue",
          auth: {
            user: process.env.sendinBlue__email,
            pass: process.env.sendinBlue__key,
          },
        });

        const mailOptions = {
          from: process.env.sendinBlue__email,
          to: email,
          subject: "Account Activation",
          html: `

                <h1>Activate your account by clicking on link below<h1>
                <a href="http://localhost:5000/account-activation/${savedUser._id}" target="_blank">Account activation</a>
        
        
            `,
        };

        transporter.sendMail(mailOptions, (error, res) => {
          if (error) {
            return response.status(500).json({
              msg: `Network error please try again later, if error continues contact ${process.envsendinBlue__email}`,
            });
          }

          return response.status(201).json({
            msg: `Email has been sent to ${email} for Account activation`,
          });
        });
      });
    } catch (error) {
      return response.status(500).json({
        msg: `Network error please try again later, if error continues contact ${process.envsendinBlue__email}`,
      });
    }
  }
}

export default userAuth;

下面的代码是我在路由中调用控制器的方式

import express from "express";
import userAuth from "../../Controller/UserAuthController/UserAuth";
const router = express.Router();

const userAuthController = new userAuth();

router.post("/api/user-signup", (request, response) => {
  userAuthController.SignUp(request, response);
});

export default router;

我可以就我为什么得到404寻求帮助吗

图片显示了当我试图以最基本的方式到达路线而没有在正文中发送任何内容时,希望我会得到回复说所有字段都必须从服务器输入

邮递员形象

标签: node.jsmodel-view-controller

解决方案


推荐阅读