Nodejs+express静的リソースへのアクセス
2276 ワード
あるリソースにアクセスしようとしたとき、アクセスできないことに気づいた.
方法1、これを加えるとアクセスできますが、staticパラメータは静的ファイル格納ディレクトリ:_dirname代表ディレクトリ
方法2、一を挙げて二を返す:パラメータをpublicに変更すると
アクセスパスは次のとおりです.http://localhost:3000/images/pic.jpg
方法3、仮想が存在しないディレクトリにアクセスしたい場合は?
アクセスパスは次のとおりです.http://localhost:3000/static/images/pic.jpg
4、質問:このように書くとアクセスパスはどうなりますか?
答えは次のとおりです.
http://localhost:3000/static/images/pic.jpg
http://localhost:3000/static/public/images/pic.jpg
利用可能path.join()継ぎ目

方法1、これを加えるとアクセスできますが、staticパラメータは静的ファイル格納ディレクトリ:_dirname代表ディレクトリ
app.use(express.static(__dirname));

方法2、一を挙げて二を返す:パラメータをpublicに変更すると
app.use(express.static('public'));
アクセスパスは次のとおりです.http://localhost:3000/images/pic.jpg

方法3、仮想が存在しないディレクトリにアクセスしたい場合は?
app.use('/static', express.static('public'));
アクセスパスは次のとおりです.http://localhost:3000/static/images/pic.jpg

4、質問:このように書くとアクセスパスはどうなりますか?
app.use('/static', express.static('public'));
app.use('/static', express.static(__dirname));
答えは次のとおりです.
http://localhost:3000/static/images/pic.jpg
http://localhost:3000/static/public/images/pic.jpg
利用可能path.join()継ぎ目
app.use('/static',express.static(path.join(__dirname,'public')));