arshaikh0171
arshaikh0171

Reputation: 1

POST request failing -React Application

please see codes: I think the code is logical but there is something wrong with front end 'onFinish' function. I have checked the api routes and they seem ok.. so slightly confused

register frontend page (Route: users)

    const router = require('express').Router()
const User = require('../models/usersModel')
const bcrypt = require('bcryptjs')

// Register new User
router.post('/register', async (req, res) => {
  try {
    const existingUser = await User.findOne({ email: req.body.email })
    if (existingUser) {
      return res.send({
        message: 'User already on database',
        success: false,
        data: null,
      })
    }

    const hashedPassword = await bcrypt.hash(req.body.password, 10)
    req.body.password = hashedPassword
    const newUser = new User(req.body)
    await newUser.save()
    res.send({
      message: 'New user created successfully',
      success: true,
      data: null,
    })
  } catch (error) {
    res.send({
      message: error.message,
      success: false,
      data: null,
    })
  }
})
module.exports = router

userRoute (backend):

const router = require('express').Router()
const User = require('../models/usersModel')
const bcrypt = require('bcryptjs')

// Register new User
router.post('/register', async (req, res) => {
  try {
    const existingUser = await User.findOne({ email: req.body.email })
    if (existingUser) {
      return res.send({
        message: 'User already on database',
        success: false,
        data: null,
      })
    }

    const hashedPassword = await bcrypt.hash(req.body.password, 10)
    req.body.password = hashedPassword
    const newUser = new User(req.body)
    await newUser.save()
    res.send({
      message: 'New user created successfully',
      success: true,
      data: null,
    })
  } catch (error) {
    res.send({
      message: error.message,
      success: false,
      data: null,
    })
  }
})
module.exports = router

userModel (backend):

const mongoose = require('mongoose')
const userSchema = new mongoose.Schema(
  {
    name: { type: String, required: true },
    email: { type: String, required: true },
    password: { type: String, required: true },
    isAdmin: { type: Boolean, default: false },
  },
  { timestamps: true }
)

module.exports = mongoose.model('users', userSchema)

server.js(backend)

const express = require('express')
const app = express()
require('dotenv').config()
const dbConfig = require('./config/dbConfig')

const port = process.env.PORT || 5000
app.use(express.json())

const usersRoute = require('./routes/usersRoute')

app.use('/api/users', usersRoute)

app.listen(port, () => console.log(`App listening on port ${port}`))

package.json

  {
  "name": "mernbus",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.0",
    "mongoose": "^7.0.3",
    "nodemailer": "^6.9.1",
    "nodemon": "^2.0.22",
    "react-process-env": "^0.1.0"
  },
  "proxy": "https://localhost:5000/"
}

ISSUE:

when registering the new user. I am getting meesage 404 not found. Request URL: http://localhost:3000/api/users/register Request Method: POST Status Code: 404 Not Found

Upvotes: 0

Views: 52

Answers (1)

Saagar Takhi
Saagar Takhi

Reputation: 1

Apart from the issue that NO frontend code is shared!

I see that the default port is 5000 if you have not supplied a port number 3000 as an env variable to your script running this server (not mentioned in question).

If that would be the case, then you need to change http://localhost:3000/api/users/register to http://localhost:5000/api/users/register

Upvotes: 0

Related Questions