프로그래밍/Node.js

Node.js 생활코딩 정리 – 웹앱 제작 파일로 된 본문 읽기

가카리 2016. 11. 27. 15:28
반응형

웹앱 제작5 : 파일로 된 본문 읽기

 

 

//글을 클릭했을때

app.get('/topic/:id', function(req, res){

var id = req.params.id;//id 접근을

//다음부분은 topics:files때문에 그런것임

fs.readdir('data', function(err, files){//첫번째는 에러 두번째는 파일목록을 전송받음

if(err){//에러가 있다면 여기서 처리

res.status(500).send('Internal Server Error');

}

fs.readFile('data/'+id, 'utf8', function(err, data){

if(err){//에러가 있다면 여기서 처리

res.status(500).send('Internal Server Error');

}

//data 파일의 내용

res.render('view', {title:id, topics:files, description:data})

});//readFile

});//readdir

});

 

 

jade 파일

 

doctype html

html

head

meta(charset='utf-8')

body

h1 server side javascript

ul

each topic in topics

li

a(href='/topic/'+topic)= topic

article

h2= title

= description

 

 

출력 화면

여기서 글 클릭시

클릭후 URL이 바뀌면서 jade파일의 article부분도 출력이 된다. 이유는 render함수에서 값이 넘어갔기 때문에..

 

 

 

출처 : https://opentutorials.org/course/2136

 

반응형