私は自分のコードを、作成できる最もシンプルな Express-js アプリにまで縮小しました。
var express = require("express"),
app = express.createServer();
app.use(express.static(__dirname + '/styles'));
app.listen(3001);
私のディレクトリは次のようになります:
static_file.js
/styles
default.css
しかし、アクセスするとhttp://localhost:3001/styles/default.css
次のエラーが発生します。
Cannot GET / styles /
default.css
express 2.3.3
と を使用していますnode 0.4.7
。何が間違っているのでしょうか?
ベストアンサー1
試すhttp://localhost:3001/default.css
。
/styles
リクエスト URL に含めるには、次を使用します。
app.use("/styles", express.static(__dirname + '/styles'));
例を見てくださいこのページ:
//Serve static content for the app from the "public" directory in the application directory.
// GET /style.css etc
app.use(express.static(__dirname + '/public'));
// Mount the middleware at "/static" to serve static content only when their request path is prefixed with "/static".
// GET /static/style.css etc.
app.use('/static', express.static(__dirname + '/public'));