-1

I´m working on a project with Node.js API, that have a frontend and a backend. Ich habe already a API-folder inside the frontend, that

await for the get request from the backend and that works for the first method like this:

export const getChange= async () => {

  return await axiosDefault.get(controller);
};

Inside the Backend I habe a folder for the Controller.js, index.js and routes.js

So the API comes to the routes and the route link the request to the controller like this:

router.get(
    "/",
    controller.getChange
);

And the controller take the request:

exports.getChange = async (req, res, next) => {
    ...

       console.log("test getChange");

}

That works completely fine.

But now I want a additional get request route to the controller for the api request getOldNames that I declared like this:

export const getOldNames= async()=> {

}

Now I have to specify the route for this request, but how?

I declared a route like this:

router.get(
    "/oldnames",
    controller.getOldNames
);

and the controller:

exports.getOldNames = async (req, res, next) => {


...

console.log("test get old names")
}

That doesn´t work this way. I want a different route on the controller. What I have to build this to make it happen? I don´t understand the route mechanic on this point on this point. Why I can on the first request just only pass a "/" and it works. But if I add the "oldnames", it doesn´t work at all? Maybe it is not possible like this, and I have to add a new Controller.js that handles that new get request?

I hope i became clear about my Problems :)

Thank you all !

I have declared some alternative routes with different parameters on the route, also on the controller and api. But nothing work like I want.

maladez
  • 1
  • 1

1 Answers1

0

I found the solution for the answer:

I have to declare another path on the api, to the route that I need for the get request.

For example, if I want to access this routes:

router.get(
    "/",
    controller.getChange
);


router.get(
    "/oldnames",
    controller.getOldNames
);

module.exports = (app) => {
    app.use("/controllerFolder", router);
};

Than I have to declare the API methods like this:

export const getNameChange = async () => {
  return await axiosDefault.get("controllerFolder");
}


export const getOldNames = async() => {
  return await axiosDefault.get("controllerFolder/oldnames");
}

Now it works fine :)

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
maladez
  • 1
  • 1