Home > Software engineering >  I am getting an error 404 using nodeJS and express
I am getting an error 404 using nodeJS and express

Time:05-08

I'm having an issue with the routes in my project called review. My other routes have no issue so I'm not sure where I went wrong here. I keep getting error 404 in my frontend and in postman. I believe everything is linking to the right information. I go the route

http://localhost:8080/api/review/addReview and get the 404 error

This is my server.js

const express = require("express");
const cors = require("cors");
const dbConfig = require("./app/config/db.config");

const app = express();

var corsOptions = {
  origin: "http://localhost:8081"
};

app.use(cors(corsOptions));

// parse requests of content-type - application/json
app.use(express.json());

// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));

const db = require("./app/models");
// const Role = db.role;

app.use('/uploads', express.static('uploads'));

db.mongoose
  .connect(`mongodb srv://[email protected]/shop?retryWrites=true&w=majority`, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false
  })
  .then(() => {
    console.log("Successfully connect to MongoDB.");
    // initial();
  })
  .catch(err => {
    console.error("Connection error", err);
    process.exit();
  });

// routes
// require(".app/routes/favourite.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);

app.use('/api/admin', require('./app/routes/admin.routes'));
app.use('/api/review', require('./app/routes/review.routes'));

// set port, listen for requests
const PORT = 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

My routes file

const express =  require('express');
const router = express.Router();
const {authJwt} = require("../middlewares");
const Review = require("../models/review.model")

router.use(function(req, res, next) {
      res.header(
        "Access-Control-Allow-Headers",
        "x-access-token, Origin, Content-Type, Accept"
      );
      next();
});


router.post("/addReview", [authJwt.verifyToken], (req, res) => {
    const review = new Review(req.body)

    review.save((err, review) => {
        if(err) return res.json({success:false, err})
        
        Review.find({'_id': review._id})
        .populate('author')
        .exec((err, result) => {
            if(err) return res.json({success: false, err})
            return res.status(200).json({success: true, result})
        })
    })
    
})

module.exports = router ;

My review model file

const mongoose = require('mongoose');

const Review = mongoose.model(
  "Review",
  new mongoose.Schema({
    prodID: String,
    productTitle: String,
    reviewId: String,
    content: String,
    author: [
      {
        type: mongoose.Schema.Types.ObjectId,
        ref: "User",
      },
    ]
  })
);

module.exports = Review;

CodePudding user response:

Everything looks ok. Your api/review/addReview route method is POST, so please check you are making post request from Postman .

  • Related