Home > OS >  Static files not showing on express server
Static files not showing on express server

Time:02-04

I am currently trying to get my JS express server to display my static HTML files using this code.

My JS Express code

**When I run my server I get this error message Error: ENOENT: no such file or directory, stat: and the directory path to the file. the directory path exists so I don't understand why it wont show the file and its contents. could someone explain this to me please. **

What I have tried in addition to my written code.

**I have tried changing the location of my project entirely and that didn't work. I have tried using app.use, __Fileneme instead of __dirname, rewriting the code nothing works.

Im using Node. js I have express installed and required I have rewritten the code several times to be sure the error was not syntax error "although VS code would have made me aware of any syntax errors" I just at a loss now. this is a simple server that should display my html files just fine yet nothing I do works.**

CodePudding user response:

try adding this line at the top: // where /public is the folder where 'welcome.html' is

 app.use('/public', express.static(__dirname '/../public'));

the npm path package may be helpful as well

app.get('/Home', (req, res) => {
  console.log(path.join(__dirname ,'/public/welcome.html'));
  res.sendFile(path.join(__dirname ,'/public/welcome.html'));
})

CodePudding user response:

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



app.use(express.static('public',{
  extensions: ['html', 'htm']
}));

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});
  • Related