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
볡μ¬