简体   繁体   中英

Express public directory cannot be found

I'm attempting to use Node.js with Express, and I read about the ability to declare a 'public' directory to serve static files out of. I'm attempting to put my bootstrap css/js files in the public directory, but I keep getting 404s.

In express:

app.use(express.static(path.join(__dirname, '../public')));
console.log(path.join(__dirname, '../public'));
//Output: D:/app/public

In HTML:

<script src="http://localhost:6969/app/public/resources/bootstrap/js/bootstrap.js"></script>

Now in my filesystem:

D:\app\public //The folder
D:\app\public\resources\bootstrap\js\bootstrap.js //The file that the 404 is on

I don't know what else I'm missing. I think I might misunderstand the app.use, or the way this public folder is supposed to work.

When you declare a public folder with express, all static files are fetched with that public folder as the root('/'). To remedy this issue,
Change:
<script src="http://localhost:6969/app/public/resources/bootstrap/js/bootstrap.js"></script>

To:
<script src="/resources/bootstrap/js/bootstrap.js"></script>

尝试这个:

<script src="resources/bootstrap/js/bootstrap.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM