Home > database >  Express GET route will not work with parameters or additionnal path
Express GET route will not work with parameters or additionnal path

Time:10-12

I have an issue I can't make a fetch request when i try to add additional path or params...

THis is what I want to :

const fetchOwnerCardList = () => {
  fetch("http://localhost:5000/api/card/ownerCards", {
    method: "GET",
    headers: {
      authorization: `Bearer ${localStorage.getItem("token")}`,
    },
  })
    .then((reponse) => reponse.json())
    .then((allSpikemmon) => {
      console.log(allSpikemmon);.................................
.............
.............

If a do exactly the same using fetch("http://localhost:5000/api/card/") it works perfectly...

I am really confused. Here is my route file :

const express = require("express");
const router = express.Router(); //création d'un routeur express dans lequel on va enregistrer nos routes

//Importation controllers
const stuffCtrl = require("../controllers/card");
const auth = require("../middleware/auth"); //auth
const multer = require("../middleware/multer"); //gestions des images
//Routes

router.post("/", auth, multer, stuffCtrl.createCard); //Création de card
router.get("/:name", stuffCtrl.getOneCard); //recup une card

router.get("/ownerCards", auth, stuffCtrl.getOwnCards);

module.exports = router; //exportations de notre routeur

and in my server.js file I have this line app.use("/api/card", CardsRoutes);

SO the problem come from the client server side but i can't figure out what it is.

Thanks for helping me, I am a beginner !

CodePudding user response:

router.get("/:name") will match /ownerCards which is a problem. Put the /ownerCards route first like this:

router.post("/", auth, multer, stuffCtrl.createCard); //Création de card

router.get("/ownerCards", auth, stuffCtrl.getOwnCards);
router.get("/:name", stuffCtrl.getOneCard); //recup une card

Remember that /:name matches anything at that path level. I call it a wildcard route so if you have any other routes at that level, then you need to make sure they are before the greedy wildcard route definition.

CodePudding user response:

Thanks so much it worked perfectly well :)

  • Related