0

So when I try to send a static page with 1 '/' in the path, for example:

app.get('/', (req, res) => {
    res.render('home.ejs')
})

It works perfectly fine and renders all the necesery css that is linked to the HTML page via the <link> attribute: <link rel="stylesheet" href="style.css">

The HTML and CSS files are in the same directory and the server.js file is in the previous directory: File directory

The server file is linked to the views directory via the:

app.use(express.static(__dirname + '/views'));
app.set('views', path.join(__dirname, 'views'));

But when I add another '/' in the path the css just won't render:

app.get('/projects/mcpaint', (req, res) => {
    res.render('mcpaint.ejs')
})

I've tried including the css in the html using the <style> tag and in that case the css is actually rendered.

Does anybody know how to fix that? Thanks, any help is appreciated!

3
  • 1
    You have probably the link to the css wrong... Hard to tell without the HTML head Commented Mar 31, 2022 at 20:30
  • Can you check the Developer Tools console in your browser and see what the error messages are indicating? They will probably show you what path you are trying to load and it will probably give you some clues as to why this is happening. Commented Mar 31, 2022 at 20:31
  • IT says GET http://localhost:3000/projects/style.css net::ERR_ABORTED 404 (Not Found) Commented Mar 31, 2022 at 21:03

1 Answer 1

2

Since you're not showing your template, all I can do is refer to my crystal ball, which says you're doing something like

<link rel="stylesheet" href="public/style.css">

which is relative to whatever the URL of the view is; in other words, it'd request /projects/public/style.css if you're viewing /projects/projectx. Instead, you'd want

<link rel="stylesheet" href="/public/style.css">

to make it absolute to the server root.

Sign up to request clarification or add additional context in comments.

7 Comments

I wasn't clear, sorry. The CSS and HTML files are in the same directory. <link rel="stylesheet" href="style.css">. The server code is in the previous directory and has nothing to do with directories, its the URL/server path that is breaking stuff
@GttMone Instead of trying to add code to comments, please improve your question. Show the HTML that you are using to render it instead of saying things like "I've tried including the css in the html using the <style> tag and in that case the css is actually rendered." Please read stackoverflow.com/help/minimal-reproducible-example
So make that /style.css and you're good.
I edited the question i hope is more clear
The location of the files on disk doesn't matter, if your server serves them from the root. Did you try /style.css in your link tag?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.