0

I want create a virtual path prefix (where the path does not actually exist in the file system) in my node js file, but it doesn't work.

The virtual path not create and my css and js not loaded from index.html

This is structure my folder

I only use app.js and static folder I also already install express.

in my app.js the code like this

app.use('/public', express.static(path.join(__dirname, 'static')));
app.get('/',(req,res)=>{
    res.sendFile(path.join(__dirname,'static','index.html'));
});
app.listen(3000);

I want to create virtual path '/public' but won't work, and my 'index.html' cant loaded my js file and css file. This is my index.html code.

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="/static/css/main.css">
</head>
<body>
    <h1>Some Html File </h1>
    <script src="/static/js/main.js" type="text/javascript"></script>
</body>
</html>

if i change in app.use('/public', express.static(path.join(__dirname, 'static'))); to app.use('/static', express.static(path.join(__dirname, 'static'))); it work correctly (my js, and css file is loaded) but my virtual path not created.

where I do wrong?

thanks

Dip Hasan
  • 225
  • 3
  • 9
froz
  • 61
  • 6

1 Answers1

0

My bad there is some miss in index.html it should be href and src with /public (virtual path) that i created in app.js

froz
  • 61
  • 6