前提・実現したいこと
当方Web構築に関して右も左もわからない状態なので、
拙い質問であることご容赦ください。
静的ファイルの提供を行いつつ、
app.get関数を読み込みたいと考えております。
root
├public
│ └index.html
└server.js
発生している問題・エラーメッセージ
①下記ソースコードで、
http://localhost:3000/index.html
にアクセスすると、index.htmlのページが表示されますが、
app.getのログ"test"が通りません。
②public直下のindex.htmlのファイル名を、
hoge.htmlなどに変更すれば、
index.htmlは読み込むことができませんが、
app.getのログ"test"は通ることができます。
理解が浅く、何が起きているのかわからない状態です。
直接htmlファイルにアクセスができれば、app.getまで処理が届かないのでしょうか。
該当のソースコード
js
1//server.js 2const express = require('express') 3const app = express() 4app.use(express.static('public')); 5 6app.listen(3000, function () { 7 console.log('Example app listening on port 3000!') 8}); 9 10app.get('/index.html', function (req, res) { 11 console.log("test"); 12});
追記
代替案として、
express.staticの使用を止め、直接ソースコードでhtmlを呼び出しています。
app.get('/', function(req, res, next){ res.sendFile(__dirname + '/public/index.html'); console.log("test"); });
補足情報
Express 4.16.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/04 06:30