Serving static files in express

Hello everyone, sorry to disturb you. I have a question regarding app.get(express.static()). Let's say we have a folder named public, and the path to this folder is passed as an argument. We noticed that if we have a stylesheet in this folder, the link to it in the HTML file doesn't need to be changed. When the server is running, the CSS is still parsed correctly. However, if we try to open the HTML file directly using the local server VS Code extension, it doesn't work. Could anyone explain why this happens please.
2 Replies
glutonium
glutonium2mo ago
u should probably show what u r saying
Faker
FakerOP2mo ago
//serve static files
app.use(express.static(path.join(__dirname, 'public'))); //This mean that we are serving static file from the public folder meaning our style sheets are in there
//serve static files
app.use(express.static(path.join(__dirname, 'public'))); //This mean that we are serving static file from the public folder meaning our style sheets are in there
<link rel="stylesheet" href="styles/style.css" />
<link rel="stylesheet" href="styles/style.css" />
Notice that our link doesn't contain the public folder but our server is still correctly serving the file but if we try to open the HTML file with live server for e.g, we won't have access to the style sheet because it is incorrectly being referenced
Want results from more Discord servers?
Add your server