0

I have an Express.js project wich looks like this:

  • public
    • images
    • javascripts
    • stylesheets
  • routes
    • index.js
  • views
    • authentication.html
    • authentication.css

In routes/index.js I can display my html file like this:

app.get('/', function(req, res) {
  res.sendFile(path.join(__dirname + '/../views/authentication.html'));
});

But the file is displayed without its style contained in authentication.css. However, authentication.css is linked to authentication.html :

<head>
    <meta charset="utf-8">
    <link rel = "stylesheet" href = "authentication.css">
</head>

I tried to move my .css to the public/stylesheets folder and change the <link> tag to point to the new location but it didn't worked.

What am I supposed to do to apply my css files to the corresponding html files?

aurelienC
  • 1,113
  • 3
  • 12
  • 22

2 Answers2

1

So I found some sort of solution thanks to this post : https://stackoverflow.com/a/17911882/5016201 .

I just added this line in app.js:

app.use(express.static(__dirname + 'views'));

It displays my html + css files correctly, I don't know if it is a good practice but it is good enough for what I am doing.

Community
  • 1
  • 1
aurelienC
  • 1,113
  • 3
  • 12
  • 22
0

Not sure if this will work but try using: =

<link rel="stylesheet" type="text/css" href="authentication.css">

try not using whitespace.

u can also try going to the route first (based on the list above)

    <link rel="stylesheet" type="text/css" href="../views/authentication.css">

When it is being run it might be using the JS as the default directory so if the first answer dont work use second

let me know if it works :D

Phil
  • 49
  • 9