我的應用程序的目錄結構:express.static(__目錄名稱+ '/公共')沒有工作
app
-views
-index.html
-article.html
-public
-stylesheet.js
而且我的編碼:
var express = require('express');
var app = express();
var hbs = require('hbs');
var blogEngine = require('./blog');
app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.json(), express.urlencoded());
app.use(express.static(__dirname+'/public'));
app.get('/', function(req, res) {
res.render('index',{title:"My Blog", entries:blogEngine.getBlogEntries()});
});
app.get('/article/:id', function(req, res) {
var entry = blogEngine.getBlogEntry(req.params.id);
res.render('article',{title:entry.title, blog:entry});
});
app.listen(8888);
使用時localhost:8888
,該stylesheet.js是很好的加載。但是當我使用localhost:8888/article.html
時,樣式表不加載。 我跟着article.html
文件{{title}}
作品但當我嘗試看看樣式表的代碼,我看到了錯誤的文字:
TypeError: Cannot read property 'title' of undefined
爲什麼entry.title
是undefined
的樣式表(除了索引頁)?
您的article.html的外觀如何? – mscdex
{{blog.title}}
發佈時間:{{blog.published} } {{blog.body}} – Rejaul非常感謝@mscdex。它正在處理'/ stylesheet.css' – Rejaul