Home > Software design >  Why the script file is on my req.params how to get rid of that?
Why the script file is on my req.params how to get rid of that?

Time:10-03

I'm trying to extract the :rid to my req.params but I get two values the rid and the script file.

here is the console.log picture

console.log picture

my route code

router.get('/room/:rid', async (req, res) => {
  console.log(req.params);
  res.render('room', { roomId: req.params.rid });
});
 

my dashboard code to redirect the user to room.ejs when the user click

 function goto(){
    var roomID = document.getElementById("roomID").value;

    window.location.replace(`/room/${roomID}`);

 }

and my room.ejs head

<script src="this.js" defer></script>

CodePudding user response:

Change this:

<script src="this.js" defer></script>

to this:

<script src="/this.js" defer></script>

Assuming that script was present in a web page called /room, then when the browser sees a request for "this.js", it will combine that with the path of the page and ask for /room/this.js which will hit your /room/:rid route which is not what you want.

Generally, you always want script file paths to be absolute paths (start with a /) so they are fixed and not dependent upon the path of the page they are contained in .

  • Related