A
A
Artyom2020-07-05 17:12:35
Express.js
Artyom, 2020-07-05 17:12:35

Why are styles and scripts not loaded with a dynamic url?

Good afternoon, if you make such a route:

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

, everything is working. But if you make it dynamic:
app.get("/work/:id", (req, res) => {
    return res.render("work");
});

On the /work/1 page, for example, styles and scripts are not loaded, except for those that are connected by cdn. Instead, the server issues html code both as a script and as css. Now I also noticed what happens everywhere if the url has a depth of more than one (for example, /work/somework also styles and scripts are not loaded). How can I solve my problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FinGanapre, 2020-07-05
@airman66

I think you are giving away static resources incorrectly. Here is the documentation
If the templates contain the correct path to static files and in express you configured the return of statics correctly, according to the documentation, then there will be no problems.
If you used this option:

app.use(express.static(path.join(__dirname, 'public')))

Then in the templates, the paths to the folders with css and js should be:
<link rel="stylesheet" href="/css/main.css">
<script src="/js/main.js"></script>

Provided that the statics are included in index.js and the directory structure is the following:
index.js
public/css/main.css
public/js/main.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question