skip to Main Content

As said in the title, I’m writing a aplication in Node.Js and I need it to return a JSON (or at least console.log() the data, I can figure out the rest) I it seems like the URL doesn’t exist

BACKEND CODE

app.get("/ranking",(req,res)=>{
    res.render("ranking");
})

app.get("registros",()=>{
    db.all("SELECT * FROM resultados", (err, rows) => {
        if (err) {
          console.error(err.message);
          return;
        }
        console.log(rows);
        return rows;
    });
})

FRONTEND CODE


function traerRegistrosDB(){

    fetch("/registros")
    .then(res=>res.json())
    .then(res => res)
    .catch(()=>{
        console.error("registros innacesibles")
    })
};

ERROR
http://localhost:3000/registros 404 (Not Found)

-I tried doing a console.log() in the back. It didn’t show up

-Adding headers

-Going directly to the direction local…./registros

2

Answers


  1. You miss a slash :
    app.get("registros" should be app.get("/registros"

    Login or Signup to reply.
  2. Since the backend and frontend addresses are different, you need to enter the full address when using fetch.

    I am sharing the example below.
    Suppose the backend address is http://localhost:8080.

    function traerRegistrosDB(){
    
        fetch("http://localhost:8080/registros")
        .then(res=>res.json())
        .then(res => res)
        .catch(()=>{
            console.error("registros innacesibles")
        })
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search