Search

Router

Router

μ •μ˜

Express μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ λΌμš°νŒ…μ„ κ΅¬ν˜„ν•˜λŠ” 객체
λΌμš°ν„° κ°μ²΄λŠ” Express μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ λΌμš°νŒ…μ„ κ΅¬ν˜„ν•˜λŠ” 데 μ‚¬μš©λ©λ‹ˆλ‹€. λΌμš°ν„°λ₯Ό μ‚¬μš©ν•˜μ—¬ HTTP μš”μ²­μ„ νŠΉμ • ν•Έλ“€λŸ¬λ‘œ 맀핑할 수 μžˆμŠ΅λ‹ˆλ‹€. ν•Έλ“€λŸ¬λŠ” μš”μ²­μ„ μ²˜λ¦¬ν•˜κ³  응닡을 μƒμ„±ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.

μ‚¬μš©λͺ©μ 

β€’
HTTP μš”μ²­μ„ νŠΉμ • ν•Έλ“€λŸ¬λ‘œ λ§€ν•‘ν•©λ‹ˆλ‹€.
β€’
URL νŒ¨ν„΄κ³Ό λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ λΌμš°νŒ…μ„ κ΅¬ν˜„ν•©λ‹ˆλ‹€.
β€’
미듀웨어λ₯Ό μ‚¬μš©ν•˜μ—¬ λΌμš°νŒ…μ„ ν™•μž₯ν•©λ‹ˆλ‹€.

μ£Όμš” 속성 및 λ©”μ†Œλ“œ

속성
μ„€λͺ…
app
λΌμš°ν„°κ°€ μ—°κ²°λœ Express μ• ν”Œλ¦¬μΌ€μ΄μ…˜
use(middleware)
λΌμš°ν„°μ— 미듀웨어λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.
get(path, handler)
HTTP GET μš”μ²­μ— λŒ€ν•œ ν•Έλ“€λŸ¬λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
post(path, handler)
HTTP POST μš”μ²­μ— λŒ€ν•œ ν•Έλ“€λŸ¬λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
put(path, handler)
HTTP PUT μš”μ²­μ— λŒ€ν•œ ν•Έλ“€λŸ¬λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
delete(path, handler)
HTTP DELETE μš”μ²­μ— λŒ€ν•œ ν•Έλ“€λŸ¬λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
all(path, handler)
λͺ¨λ“  HTTP μš”μ²­μ— λŒ€ν•œ ν•Έλ“€λŸ¬λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
param(name)
URL νŒ¨ν„΄μ—μ„œ λ§€κ°œλ³€μˆ˜λ₯Ό μΆ”μΆœν•˜λŠ” ν•¨μˆ˜λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.
paramNames()
URL νŒ¨ν„΄μ—μ„œ μ‚¬μš©λ˜λŠ” λͺ¨λ“  λ§€κ°œλ³€μˆ˜ 이름을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

κ²Œμ‹œνŒ μ˜ˆμ‹œμ½”λ“œ

β€’
app.js
β€’
./routes/index.js
β€’
./routes/users.js
β€’
./views/board/list.html
β€’
./views/board/read.html
β€’
./views/board/insert.html
β€’
./views/board/update.html

app.js

// λͺ¨λ“ˆ import const express = require('express') const morgan = require('morgan') const cookieParser = require('cookie-parser') const session = require('express-session') const path = require('path') // λΌμš°ν„° import const indexRouter = require('./routes/index') // 미듀웨어 μ„€μ • const app = express() const port = 3000 app.set('port', port) // 정적 파일 경둜 지정 app.use('/', express.static(path.join(__dirname, 'public'))) app.use(morgan('dev')) app.use(express.json()) app.use(express.urlencoded({ extended: false })) app.use(cookieParser()) app.use(session({ secret: 'your-secret-key', resave: false, saveUninitialized: true })); // λΌμš°ν„° μ„€μ • app.use('/', indexRouter) // μœ„μ˜ λ§€ν•‘λ˜μ§€ μ•Šμ€ λ‚˜λ¨Έμ§€ μš”μ²­ app.use((req, res, next) => { res.status(404).send('NOT FOUND') }) // μ—λŸ¬ ν•Έλ“€λŸ¬ app.use((err, req, res, next) => { console.error(err); res.status(500).send(err.message) }) // 포트 μ„€μ • 및 μš”μ²­ λŒ€κΈ° app.listen(app.get('port'), () => { console.log(app.get('port'), '번 ν¬νŠΈμ—μ„œ λŒ€κΈ° 쀑'); })
JavaScript
볡사

./routes/index.js

const express = require('express') const router = express.Router() router.get('/', (req, res) => { res.send('Main') }) module.exports = router
JavaScript
볡사

./routes/users.js

const express = require('express'); const router = express.Router(); // GET /user λΌμš°ν„° router.get('/', (req, res) => { res.send('Hello, User'); }); module.exports = router;
JavaScript
볡사

./routes/board.js

const express = require('express') const router = express.Router() let boardList = [ { title : '제λͺ©1', writer : 'μž‘μ„±μž1', content : 'λ‚΄μš©1' }, { title : '제λͺ©2', writer : 'μž‘μ„±μž2', content : 'λ‚΄μš©2' }, { title : '제λͺ©3', writer : 'μž‘μ„±μž3', content : 'λ‚΄μš©3' }, ] // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ λͺ©λ‘ router.get('/', (req, res) => { res.render('board/list', {boardList} ) }) // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ 등둝 router.get('/insert', (req, res) => { res.render('board/insert') }) // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ 등둝 router.post('/', (req, res) => { // ꡬ쑰뢄해할당 const { title, writer, content } = req.body; // const title = req.body.title // const writer = req.body.writer // const content = req.body.content const newBoard = { title, writer, content }; boardList.push(newBoard); res.redirect('/board'); }); // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ μˆ˜μ • νŽ˜μ΄μ§€ router.get('/update/:id', (req, res) => { console.log(`id : ${req.params.id}`); let id = req.params.id const board = boardList[id]; res.render('board/update', { board, id }); }); // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ μˆ˜μ • router.post('/update', (req, res) => { const { id, title, writer, content } = req.body; boardList[id] = { title, writer, content }; res.redirect(`/board/${id}`); }); // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ μ‚­μ œ router.post('/delete', (req, res) => { const id = req.body.id; console.log('μ‚­μ œ : ' + id); boardList.splice(id, 1); res.redirect('/board'); }); // πŸ‘©β€πŸ’» κ²Œμ‹œκΈ€ 읽기 // μš”μ²­ κ²½λ‘œμ— νŒŒλΌλ―Έν„° 맀핑 방법 ➑ '/:νŒŒλΌλ―Έν„°λͺ…' router.get('/:id', (req, res) => { console.log(`id : ${req.params.id}`); let id = req.params.id let board = boardList[id] res.render('board/read', {board, id}) }) module.exports = router;
JavaScript
볡사

./views/board/list.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ λͺ©λ‘</title> </head> <body> <h1>κ²Œμ‹œκΈ€ λͺ©λ‘</h1> <a href="/board/insert">κΈ€μ“°κΈ°</a> <ul> {% for board in boardList %} <li> <a href="/board/{{loop.index -1}}"> <strong>{{ board.title }}</strong> </a> <p>μž‘μ„±μž: {{ board.writer }}</p> <p>{{ board.content }}</p> </li> {% endfor %} </ul> </body> </html>
HTML
볡사

./views/board/read.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ 읽기</title> </head> <body> <h1>κ²Œμ‹œκΈ€ 읽기</h1> <table border="1"> <tr> <td>제λͺ©</td> <td>{{ board.title }}</td> </tr> <tr> <td>μž‘μ„±μž</td> <td>{{ board.writer }}</td> </tr> <tr> <td>λ‚΄μš©</td> <td>{{ board.content }}</td> </tr> </table> <a href="/board/update/{{id}}">μˆ˜μ •</a> </body> </html>
HTML
볡사

./views/board/insert.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ 등둝</title> </head> <body> <h1>κ²Œμ‹œκΈ€ 등둝</h1> <form action="/board" method="post"> <input type="text" name="title" placeholder="제λͺ©"> <br> <input type="text" name="writer" placeholder="μž‘μ„±μž"> <br> <input type="text" name="content" placeholder="λ‚΄μš©"> <br> <input type="submit" value="등둝"> </form> </body> </html>
HTML
볡사

./views/board/update.html

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>κ²Œμ‹œκΈ€ μˆ˜μ •</title> </head> <body> <h1>κ²Œμ‹œκΈ€ μˆ˜μ •</h1> <form action="/board/update" method="post"> <input type="hidden" name="id" value="{{id}}"> <input type="text" name="title" placeholder="제λͺ©" value="{{board.title}}"> <br> <input type="text" name="writer" placeholder="μž‘μ„±μž" value="{{board.writer}}"> <br> <input type="text" name="content" placeholder="λ‚΄μš©" value="{{board.content}}"> <br> <input type="submit" value="μˆ˜μ •"> </form> <form action="/board/delete" method="post"> <input type="hidden" name="id" value="{{id}}"> <input type="submit" value="μ‚­μ œ"> </form> </body> </html>
HTML
볡사