NodeJS 我试图在我的应用程序中建立身份验证,当我尝试注册用户时,它说:404 Not Found Not Found Not Found Not Found

ctzwtxfj  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(115)

当我尝试注册用户时,它说:注册.jsx:49 POST http://localhost:5173/api/v1/user/register 404(Not Found)and axios error comes which is:AxiosError {message:'请求失败,状态代码为404',名称:'AxiosError',代码:'ERR_BAD_REQUEST',配置:{...},请求:XMLHttpRequest,...}
这是我的userRoutes.js代码:

const express = require('express')
const { loginController, registerController } = require('../controllers/userCtrl')

//router object
const router = express.Router()

//routes
//LOGIN || POST
router.post('/login', loginController)

//REGISTER ||POST
router.post('/register', registerController)

module.exports = router

字符串
这是我的userModel.js代码:

const mongoose = require('mongoose')

const userSchema = mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Name is required']
    },
    email: {
        type: String,
        required: [true, 'Email is required']
    },
    password: {
        type: String,
        required: [true, 'Password is required']
    },
    aadharNumber: {
        type: Number,
        required: [true, 'Aadhar number is required']
    }
})

const userModel = mongoose.model('users', userSchema)

module.exports = userModel


这是我的userController.js代码:

//import model
const userModel = require('../models/userModels')
const bcrypt = require('bcryptjs')

//register callback
const registerController = async (req, res) => {
    try {
        const existingUser = await userModel.findOne({ email: req.body.email })
        if (existingUser) {
            return res.status(200).send({
                message: 'User already exists', success: false
            })
        }
        const password = req.body.password
        const salt = await bcrypt.genSalt(10)
        const hashedPassword = await bcrypt.hash(password, salt)
        req.body.password = hashedPassword
        const newUser = new userModel(req.body)
        await newUser.save()
        res.status(201).send({
            message: 'Registered sccessfully', success: true
        })
    } catch (error) {
        console.log(error)
        res.status(500).send({
            success: false,
            message: `Register controller: ${error.message}`
        })
    }
}

//login callback
const loginController = () => {

}

module.exports = { loginController, registerController }


这是我的server.js代码:

const express = require("express");
const morgan = require("morgan");
const dotenv = require("dotenv");
const connnectDB = require("./config/db");

// dotenv config
dotenv.config();

//mongodb connection
connnectDB();

//rest object
const app = express();

// middlewares
app.use(express.json());
app.use(morgan("dev"));

// routes
app.use('/api/v1/user', require('./routes/userRoutes'))

//port
const port = process.env.PORT || 8070;

//listen port
app.listen(port, () => {
  console.log(
    `Server running in ${process.env.NODE_MODE} mode on port ${process.env.PORT}`
  );
});


这是我的根包.json:

{
  "name": "babysitease",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js",
    "server": "nodemon server.js",
    "client": "cd client && npm run dev",
    "both": "concurrently \"npm run server\" \"npm run client\""
  },
  "keywords": [],
  "author": "mustafa",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "concurrently": "^8.2.0",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.1",
    "mongoose": "^7.4.0",
    "morgan": "^1.10.0",
    "nodemon": "^3.0.1"
  }
}


这是我的frontend包。json:

{
  "name": "client",
  "proxy": "http://localhost:8070",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
    "preview": "vite preview"
  },
  "dependencies": {
    "antd": "^5.7.3",
    "axios": "^1.4.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.14.2",
    "react-toastify": "^9.1.3"
  },
  "devDependencies": {
    "@types/react": "^18.2.15",
    "@types/react-dom": "^18.2.7",
    "@vitejs/plugin-react": "^4.0.3",
    "autoprefixer": "^10.4.14",
    "eslint": "^8.45.0",
    "eslint-plugin-react": "^7.32.2",
    "eslint-plugin-react-hooks": "^4.6.0",
    "eslint-plugin-react-refresh": "^0.4.3",
    "postcss": "^8.4.27",
    "tailwindcss": "^3.3.3",
    "vite": "^4.4.5"
  }
}


这是文件夹结构:folder structure客户端文件夹用于前端
任何帮助将不胜感激!!

qnakjoqk

qnakjoqk1#

//port
const port = process.env.PORT || 8070;

//listen port
app.listen(port, () => {
  console.log(
    `Server running in ${process.env.NODE_MODE} mode on port ${port}`
  );
});

字符串
在console.log()中更改端口,看看它是否在该端口上。
我刚刚测试了你的路由,一切似乎都很好。如果只是404,可能是错误的端口。
您可以使用一个名为Postman的工具来检查这一点。

相关问题