0

I'm working in a Node.js app and I'm having trouble with a specific html page. All the pages I've done to this point are simply html and css, but in this case I have a multistep page that runs a script to move through the steps (showing diferent forms). In my case it won't load this script so the page doesn't work at all. The get request looks like this:

router.get("/reservaAula", eToken, (req, res) => {
  jwt.verify(req.token, keyDocente, (err, data) => {
    if (err) {
      res.sendStatus(404);
    } else {
      res.render("reservaAula.html");
    }
  });
});

And the page is (the important part):

<!DOCTYPE html>
<html lang="en" id = "html">
<head>
<title>Reserva de Aula</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="/js/script.js"></script>
<script src="js/reservarAula.js"></script>
</head>
<body>
...

I've placed the script in the public folder, just like I placed my css, but no matter what I try it won't work. In the rest of the pages loads the other scripts in js folder correctly (like reservarAula.js), but not with script.js.

Megan
  • 15
  • 3
  • I doubt that you can use `res.render` to render html pages like that. You have to use a library for that method to work. – Always Helping Jun 30 '20 at 07:00
  • Do you have any suggestions of a library I can use? I'm new to all this and I'm absolutely lost right now – Megan Jun 30 '20 at 07:07
  • 1
    Assuming you're using `express.static()` for your static script files, CSS files and images, please show us that line of code. Then, also show us exactly where you put the `reservarAula.js` and `script.js` files in your file system. – jfriend00 Jun 30 '20 at 07:07
  • Please read more about serving static files using express here: https://expressjs.com/en/starter/static-files.html OR This answer might help as well: https://stackoverflow.com/questions/53801317/how-to-render-html-file-with-express-js – Always Helping Jun 30 '20 at 07:11
  • Here is more detailed answer: https://stackoverflow.com/questions/4529586/render-basic-html-view You can library like ejs. – Always Helping Jun 30 '20 at 07:13
  • Thank you all for the answers, it turns out that it was not working as expected due to another thing going on in the proyect. After solving it, the dynamic page loaded the script and worked as expected. – Megan Jul 02 '20 at 00:14

1 Answers1

0

CSS and JS should be in the static folder and in your sub-folder. It should then work.

Example file location /static/css/stylesheet.css How to refer to it /css/stylesheet.css

(You can do the same with JS)

Majonez.exe
  • 412
  • 8
  • 22
  • Sorry to say but this is not the answer to the question asked by OP. Can you please add some explanation to why this is an answer ? – Always Helping Jun 30 '20 at 07:09
  • `I've placed the script in the public folder, just like I placed my css, but no matter what I try it won't work. In the rest of the pages loads the other scripts in js folder correctly (like reservarAula.js), but not with script.js.` According to my translation, the point here is that the scripts do not load. – Majonez.exe Jun 30 '20 at 07:20
  • After solving the problem that was causing the js not to load, this was the actual solution so I'll mark it as the solution. – Megan Jul 02 '20 at 00:15