c o d i n g . . ๐Ÿ‰/Node.js

[์ƒํ™œ์ฝ”๋”ฉ] Node.js ๋ฅผ ์ด์šฉํ•ด ์›น์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๊ธฐ(4)

H J 2021. 7. 12. 16:24

๊ธ€ ๋ชฉ๋ก ๋งŒ๋“ค๊ธฐ

์ €์žฅ๋œ data๋ฅผ ์ฝ์–ด์˜ค๋Š” ๋ฒ•

*GET์— ๋Œ€ํ•œ ๋ผ์šฐํŠธ

app.get('/topic', function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ fs.readdir('data', function(err, files){ if(err){ res.status(500).send('Internal Server Error'); } res.render('view', {topics: files}); }) })


ํ…œํ”Œ๋ฆฟ์—”์ง„์„ ์ด์šฉํ•œ๋‹ค
app.set('views', './views_file');
app.set('view engine', 'pug');
์ „ ์ˆ˜์—…์—์„œ ์ž‘์„ฑํ–ˆ๋˜ ์ด ์ฝ”๋“œ๊ฐ€ ์žˆ์–ด์•ผ ํ•จ

readdir์„ ์ด์šฉํ•ด์„œ data๋ฅผ ์ฝ์–ด์˜จ๋‹ค
๊ทธ๋•Œ callback ํ•จ์ˆ˜๋Š” err์™€ file ๋ชฉ๋ก(files=๋ฐฐ์—ด)์„ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค

js ํŒŒ์ผ์—์„œ pug๋กœ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•  ๋•Œ
render๋ฅผ ์ด์šฉํ•ด์„œ res.render('view', {topics: files}); ์ด๋ ‡๊ฒŒ ์ •๋ณด๋ฅผ ๋ณด๋‚ด์ค€๋‹ค
(์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ๋™์‹œ์— ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค)


์•„๋ž˜์™€ ๊ฐ™์€ view.pug ํŒŒ์ผ์„ ์ž‘์„ฑํ•œ๋‹ค
(์™„์„ฑ๋œ view.pug)

doctype html html head meta(charset="UTF-8") title Document body h1 Server Side JavaScript ul each topic in topics li a(href="/topic/"+topic)= topic article h2= title = description

๋ฐ˜๋ณต๋ฌธ( each i in val )์„ ์ด์šฉํ•ด์„œ data์— ๋งž์ถ”์–ด li, a ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค

topics๋Š” js ํŒŒ์ผ์—์„œ ๋ฐ›์•„์˜จ files ๋ชฉ๋ก์ด๋‹ค
(files๊ฐ€ ์•„๋‹Œ topics๋ฅผ ์ด์šฉํ•ด์•ผ ํ•จ)

๊ฐ ๋ชฉ๋ก์— a ํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  a ํƒœ๊ทธ์˜ ์†์„ฑ์„ ์ด์šฉํ•ด์„œ ๋งํฌ๋ฅผ ๊ฑธ์–ด์ค€๋‹ค
a(href="/topic/"+topic)= topic


๋ณธ๋ฌธ ์ฝ๊ธฐ

๋ชฉ๋ก์„ ํด๋ฆญํ–ˆ์„ ๋•Œ ์ •๋ณด๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•˜๋Š” ๋ฒ•

*๋ผ์šฐํŠธ
(์™„์„ฑ๋œ ์ฝ”๋“œ)

app.get('/topic/:id', function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ ๊ฐ list ํŽ˜์ด์ง€ ๋ผ์šฐํŠธ let id = req.params.id; // :id์˜ ๊ฐ’ fs.readdir('data', function(err, files){ if(err){ res.status(500).send('Internal Server Error'); } fs.readFile('data/'+id, 'utf-8', function(err, data){ if(err){ res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) }) })

URL์—์„œ ๋ฐ”๋€” ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์€ :์œผ๋กœ ํ‘œ์‹œํ•ด์ค€๋‹ค

let id = req.params.id;
์ด ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด id์— ์ ‘๊ทผํ•œ๋‹ค

์ ‘๊ทผํ•œ id ๊ฐ’์„ ์ด์šฉํ•ด์„œ ํŒŒ์ผ์„ ์ฝ๊ธฐ ์œ„ํ•ด readFile์„ ์ด์šฉํ•œ๋‹ค

fs.readFile('data/'+id, 'utf-8', function(err, data){ if(err){ res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); })

์ด๋ ‡๊ฒŒ js ํŒŒ์ผ์—์„œ ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ pug๋กœ ์ „๋‹ฌํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค

readdir์„ ์ด์šฉํ•ด์„œ data ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์˜ ํŒŒ์ผ๋“ค์˜ ๋ชฉ๋ก์„ ๊ฐ€์ ธ์˜จ ํ›„ readFile์„ ์ด์šฉํ•ด์„œ id ๊ฐ’์„ ์ด์šฉํ•ด์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ์ฝ๋Š”๋‹ค

์ฝ”๋“œ์˜ ๊ฐœ์„ 

*readdir ์ค‘๋ณต & view ์ค‘๋ณต

์ค‘๋ณต ์ œ๊ฑฐ -> ์œ ์ง€๋ณด์ˆ˜ ์‰ฌ์›€, ๊ฐ€๋…์„ฑ ์ข‹์Œ

app.get(['/topic', '/topic/:id'], function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ /topic ๋ผ์šฐํŠธ fs.readdir('data', function(err, files){ if(err){ res.status(500).send('Internal Server Error'); } let id = req.params.id; // :id์˜ ๊ฐ’ if(id){ // id ๊ฐ’ ์žˆ์„ ๋•Œ fs.readFile('data/'+id, 'utf-8', function(err, data){ if(err){ res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) } else{ // id ๊ฐ’ ์—†์„ ๋•Œ res.render('view', {topics: files, title: 'Wellcome', description:'Hello!!'}); } }) })

[]๋ฅผ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ path ๋ผ์šฐํŠธํ•˜๊ธฐ

if - else๋ฅผ ์ด์šฉํ•ด์„œ id ๊ฐ’์ด ์žˆ์„ ๋•Œ, ์—†์„ ๋•Œ ๋‹ค๋ฅธ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜๊ธฐ
id ๊ฐ’์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ readFile์„ ์ด์šฉํ•ด์„œ id ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ํŒŒ์ผ ์ฝ๊ธฐ

id ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋ฐ”๋กœ view์— topics, title, description ์ „๋‹ฌ

id ๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ
๋”๋ณด๊ธฐ

topic/:id ๋ผ์šฐํŠธ ์‚ญ์ œํ•˜๊ธฐ

app.get('/topic/:id', function(req, res){ // get ๋ฐฉ์‹์œผ๋กœ ๊ฐ list ํŽ˜์ด์ง€ ๋ผ์šฐํŠธ let id = req.params.id; // :id์˜ ๊ฐ’ fs.readdir('data', function(err, files){ if(err){ res.status(500).send('Internal Server Error'); } fs.readFile('data/'+id, 'utf-8', function(err, data){ if(err){ res.status(500).send('Internal Server Error'); } res.render('view', {topics:files, title:id, description:data}); }) }) })



*์ œ๋ชฉ ํด๋ฆญํ•˜๋ฉด /topic์œผ๋กœ ๊ฐ€๋„๋ก view.pug ์ˆ˜์ •

doctype html html head meta(charset="UTF-8") title Document body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href="/topic/"+topic)= topic article h2= title = description


*new ํด๋ฆญํ•˜๋ฉด .../topic/new๋กœ ์ด๋™ํ•˜๋„๋ก view.pug ์ˆ˜์ •

doctype html html head meta(charset="UTF-8") title Document body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href="/topic/"+topic)= topic article h2= title = description div a(href='/topic/new') new



*.../topic/new ๋””์ž์ธ ๋น„์Šทํ•˜๋„๋ก new.pug ์ˆ˜์ •

doctype html html head meta(charset="UTF-8") title Document body h1 a(href='/topic') Server Side JavaScript ul each topic in topics li a(href="/topic/"+topic)= topic article form(action="/topic" method="post") p input(type="text" name="title" placeholder="title") p textarea(name="description") p input(type="submit")


์ด๋•Œ js ํŒŒ์ผ์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š์œผ๋ฉด topics๋ฅผ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋œฌ๋‹ค

js ํŒŒ์ผ์˜ .../topic/new ๋ผ์šฐํŠธ ๋ถ€๋ถ„ ์ˆ˜์ •
(์›๋ž˜๋Š” render('new')๋งŒ ์žˆ์—ˆ๋Š”๋ฐ topics๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด readdir์„ ์ด์šฉํ•ด์„œ files๋ฅผ ์ฝ์–ด์˜จ๋‹ค)

app.get('/topic/new', function(req, res){ //form์„ topic/new๋กœ fs.readdir('data', function(err, files){ if(err){ res.status(500).send('Internal Server Error'); } res.render('new', {topics: files}); }) })
์ˆ˜์ • ์™„๋ฃŒ๋œ ๋ชจ์Šต



*์ •๋ณด ์ž…๋ ฅ ํ›„ ์ œ์ถœํ•˜๋ฉด ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ์ˆ˜์ •

์ž…๋ ฅ ํ›„ ์ œ์ถœ
์ˆ˜์ • ์ „: ์„ฑ๊ณตํ•˜๋ฉด Success! ๋œธ


app.post ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค
res.send('Success!');

res.redirect('/topic/'+title);
์ด๋ ‡๊ฒŒ redirect๋ฅผ ์ด์šฉํ•ด์„œ ๋ฐ”๋กœ ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜๋„๋ก ์ˆ˜์ •

์ •๋ณด ์ž…๋ ฅ
์ˆ˜์ • ํ›„: ๋ฐ”๋กœ ํ•ด๋‹น ํŽ˜์ด์ง€๋กœ ์ด๋™
ใ„ทใ… ๋“ฃ๊ธดํ–ˆ์ง€๋งŒ ๋‹ค์‹œ ์ •๋ฆฌํ•œ ๊ฑฐ ๋ณด๋ฉด์„œ ๊ณต๋ถ€ํ•ด์•ผ๊ฒŒ๋”ฐ ;ใ……;