Express Node.js アプリケーションがあります。構造は次のとおりです。
myapp
+-- node_modules
+-- public
|-- htmls
|-- myhtml.html
+-- routes
|-- index.js
|-- app.js
私のapp.js
は次のとおりです。
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
// some stuff...
app.use(express.static(path.join(__dirname, 'public')));
app.use('/public', express.static(path.join(__dirname, 'public')));
app.get('/', routes.index);
app.get('/content/:file', routes.plainhtml);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
私のroutes/index.js
は次のとおりです。
// Some stuff...
exports.plainhtml = function(req, res) {
res.sendfile('/public/htmls/' + req.params.file);
};
ブラウザを開いて次のアドレスを取得しようとすると、http://localhost:3000/content/myhtml.html
404 エラーが発生します。
Express 404 エラー: ENOENT、stat '/public/htmls/myhtml.html'
ルーティングが完了し、関数が呼び出されます...問題は、使用しようとしたときですres.sendfile
。私はそこにどのアドレスを渡せばいいですか?
何をすべきか?