LoginSignup
1
1

More than 3 years have passed since last update.

【Node.js】Express フロント側でpublicフォルダのcssやらライブラリファイルを読み込めない時の対処法

Last updated at Posted at 2020-06-26

jqueryが読み込めない・・・

image.png
index.ejsから
・public/lib/jquery-3.5.1.min.jsw
・public/css/index.css
を読み込めない!

image.png
404 Not Foundと言われていますね

なんで・・・?

index.ejs
<!-- css -->
<link rel="stylesheet" type="text/css" href="public\css\index.css">
<!-- jQuery -->
<script type="text/javascript" src="public\lib\jquery-3.5.1.min.js"></script>

ちゃんと相対パス書いてあげてるのになー
絶対パスに変えてみてもダメだった

解決策

app.js
// 静的ファイルは無条件に公開
app.use('/public', express.static('public'))

app.jsでpublicフォルダをstaticにしてあげるだけでよかった!

おわり

みなさんも同じ手詰まりをしないように

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1