LoginSignup
0
0

More than 3 years have passed since last update.

【Express】静的ファイルをホスティングする方法

Last updated at Posted at 2021-02-13

プログラミング勉強日記

2021年2月13日
静的ファイルのホスティングをする方法を間違えていたため、以下のようなエラーが出た。

net::ERR_ABORTED 404 (Not Found)

Expressで静的ファイルをホスティングする方法

 Expressで、 画像、CSSファイル、JSファイルなどの静的ファイルをホスティングするためには標準で組み込まれているexpress.staticを使用する。

関数のシグネチャ
express.static(root, [options])

 express.staticの基本的な使い方は、Applicationオブジェクトのuse()を使ってミドルウェアの設定を行う。

ファイルの構造
app.js
public/
    +-- images/
    +-- js/
    +-- css/
app.js
app.use(express.static(__dirname + '/public'));
app.jsの全体のコード
var express = require('express');
var app = express();

app.use(express.static('public', { hidden: true }));
app.listen(process.env.PORT || 7000);

参考文献

Serving static files in Express
javascript - JSファイルはnet::ERR_ABORTED 404(Not Found)を取得します

0
0
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
0
0