2014-07-04 3 views
1

내 응용 프로그램의 디렉토리 구조 :express.static (__이 dirname + '공개 /')가 작동하지 않습니다

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을 사용하면 스타일 시트가로드되지 않습니다. (인덱스 페이지 제외) 스타일 시트

TypeError: Cannot read property 'title' of undefined 

entry.title입니다 undefined : 나는 article.html 파일에 {{title}} 작품을 따라하지만 스타일 시트의 코드를 참조 할 때, 나는 오류 텍스트를 볼 수?

+0

article.html의 형식은 무엇입니까? – mscdex

+0

<링크 확인해 = "스타일"유형 = "텍스트/CSS는"HREF = "stylesheet.css">

{{blog.title}} 게시

: {{blog.published} } {{blog.body}} – Rejaul

+0

@mscdex 감사합니다. 그것은/stylesheet.css와 작동합니다. – Rejaul

답변

0

이는 그것이 그 오류가 발생 entry.title을 읽으려고 할 때 분명히 다음 undefined을 반환 var entry = blogEngine.getBlogEntry(req.params.id);

에서 함수 호출로 오히려 stylesheet.js과 관련이 보이지 않는다.

+0

스타일 시트를 index.html과 article.html로 링크 시켰습니다. article.html과 함께 stylesheet.js를로드하려면 어떻게해야합니까? – Rejaul

1

href/stylesheet.css으로 변경하십시오. /article/..을 방문하면 href이 (현재) article.html의 상대 경로이기 때문에 브라우저가 /article/stylesheet.css을 조회합니다.

관련 문제