-1

I've been looking at some of the answers on this forum but nothing seems to work.

My main HTML file refers to the style sheet as follows:

<link rel="stylesheet" href="/styles/main.css">

I have an index.js file with the following code:

const express = require('express'); 
const app = express();              
const port = 3000;

app.get('/', (req, res) => {        
    res.sendFile('index.html', {root: __dirname});                                                      
});

app.listen(port, () => {            
    console.log(`Now listening on port ${port}`); 
});

Finally, this is my folder structure:

folder structure screenshot

I'd really appreciate some guidance on this

alvivanco
  • 17
  • 3
  • 1
    You never serve the file – Konrad Oct 24 '22 at 00:25
  • Sorry if this seems super simple -- I'm new at this. I've tried to follow different of the recommended solutions in there, but still not able to make it work. This is the last change i made on the code before "app.get": `app.use(express.static(__dirname + '/styles'));` Anything else I am missing or doing wrong? – alvivanco Oct 24 '22 at 01:05
  • nevermind! it worked. I had to restart the local host through the terminal – alvivanco Oct 24 '22 at 02:42

2 Answers2

0

FIXED!

I needed to add the following code to my index.js file before the "app.get" portion:

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

NOTE: re-starting the LocalHost instance is also necessary after modifying index.js file

alvivanco
  • 17
  • 3
-1

You can serve static files using express.static, like this:

app.use('/styles', express.static('styles'));
app.get('/', ...

See this documentation, https://expressjs.com/en/starter/static-files.html .

mythosil
  • 273
  • 1
  • 9