2016-07-20 5 views
-1

Expressを使用して、index.htmlファイルを実行しますが、CSS、jsまたは画像を正しくリンクできません。画像が表示されず、CSSとjsはリンクされていません。Expressテンプレートがスクリプト、CSS、または画像にリンクしていません。

<link rel="stylesheet" type="text/css" href="css/main.css"> 

<script type="text/javascript" src="js/jquery.js"></script> 
<script type="text/javascript" src="js/main.js"></script> 

img/logo.png 

私は次のようなディレクトリ構造を持っている:app.jsで

root 
    app.js 
    package.json 
    node_modules 
    assets 
     img 
     css 
     js 
     templates 
      theme1 
       css 
       fonts 
       img 
       js 
       index.html 
       about.html 
       services.html 
       news.html 
       contact.html 

を:

var express = require('express'); 
var app = express(); 
var path = require('path'); 

app.get('/', function(req, res) { 
    //res.sendFile(path.join(__dirname + '/assets/templates/theme1/index.html')); 
}); 

app.get('/about/', function(req, res) { 
    res.sendFile(path.join(__dirname + '/assets/templates/theme1/about.html')); 
}); 

app.get('/services/', function(req, res) { 
    res.sendFile(path.join(__dirname + '/assets/templates/theme1/services.html')); 
}); 

app.get('/services/', function(req, res) { 
    res.sendFile(path.join(__dirname + '/assets/templates/theme1/news.html')); 
}); 

app.get('/contact/', function(req, res) { 
    res.sendFile(path.join(__dirname + '/assets/templates/theme1/contact.html')); 
}); 

app.listen(3000); 

app.getapp.useのより良い理解を必要とするだけでなく、res.sendFile

おかげですべての

答えて

0

これは、あなたが望むファイル(jsやcssなど)を提供するようにExpressに指示していないため動作しません。あなたはセットアップにstatic middlewareを使用してスタティックルートを望ん

まず:

その時点で

app.use(express.static(__dirname + '/assets'));

、「資産」ディレクトリにあるすべてのものは、あなたのルートURLからの相対提供されます。したがって、/img/someimage.jpgは正しいURLです。

+0

app.useを使用してapp.jsファイルを設定するにはどうすればよいですか?私はまだapp.getとapp.sendfileが必要ですか? –

+0

に依存します。/aboutのリクエストがファイルシステムのファイルをassets/templates/theme1/about.htmlに返す動作を維持したい場合は、何もしていないことを続けて、推奨される変更を追加するほうが簡単でしょうすべてのapp.get宣言の前に – Paul

+0

あなたの助けに感謝しました –

関連する問題