๋ผ์ฐํ
_๋ผ์ฐํ _์ ์ ํ๋ฆฌ์ผ์ด์ ์๋ ํฌ์ธํธ(URI)์ ์ ์, ๊ทธ๋ฆฌ๊ณ URI๊ฐ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์๋ตํ๋ ๋ฐฉ์์ ๋งํฉ๋๋ค. ๋ผ์ฐํ ์ ๋ํ ์๊ฐ๋ ๊ธฐ๋ณธ ๋ผ์ฐํ ์ ์ฐธ์กฐํ์ญ์์ค.
You define routing using methods of the Express app
object that correspond to HTTP methods;
for example, app.get()
to handle GET requests and app.post
to handle POST requests. For a full list,
see app.METHOD. You can also use app.all() to handle all HTTP methods and app.use() to
specify middleware as the callback function (See Using middleware for details).
ํน์ํ ๋ผ์ฐํ
๋ฉ์๋์ธ app.all()
์ ์ด๋ ํ HTTP ๋ฉ์๋๋ก๋ถํฐ๋ ํ์๋์ง ์์ต๋๋ค. ์ด ๋ฉ์๋๋ ๋ชจ๋ ์์ฒญ ๋ฉ์๋์ ๋ํด ํ ๊ฒฝ๋ก์์ ๋ฏธ๋ค์จ์ด ํจ์๋ฅผ ๋ก๋ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
In fact, the routing methods can have more than one callback function as arguments.
With multiple callback functions, it is important to provide next
as an argument to the callback function and then call next()
within the body of the function to hand off control
to the next callback.
๋ค์ ์ฝ๋๋ ๋งค์ฐ ๊ธฐ๋ณธ์ ์ธ ๋ผ์ฐํธ์ ์์ ๋๋ค.
const express = require('express')
const app = express()
// respond with "hello world" when a GET request is made to the homepage
app.get('/', (req, res) => {
res.send('hello world')
})
๋ผ์ฐํธ ๋ฉ์๋
๋ผ์ฐํธ ๋ฉ์๋๋ HTTP ๋ฉ์๋ ์ค ํ๋๋ก๋ถํฐ ํ์๋๋ฉฐ, express
ํด๋์ค์ ์ธ์คํด์ค์ ์ฐ๊ฒฐ๋ฉ๋๋ค.
๋ค์ ์ฝ๋๋ ์ฑ์ ๋ฃจํธ์ ๋ํ GET ๋ฐ POST ๋ฉ์๋์ ๋ํด ์ ์๋ ๋ผ์ฐํธ์ ์์ ๋๋ค.
// GET method route
app.get('/', (req, res) => {
res.send('GET request to the homepage')
})
// POST method route
app.post('/', (req, res) => {
res.send('POST request to the homepage')
})
Express๋ HTTP ๋ฉ์๋์ ํด๋นํ๋ ๋ค์๊ณผ ๊ฐ์ ๋ผ์ฐํ ๋ฉ์๋๋ฅผ ์ง์ํฉ๋๋ค. For a full list, see app.METHOD.
There is a special routing method, app.all()
, used to load middleware functions at a path for all HTTP request methods. For example, the following handler is executed for requests to the route "/secret"
whether using GET
, POST
, PUT
, DELETE
, or any other HTTP request method supported in the http module.
app.all('/secret', (req, res, next) => {
console.log('Accessing the secret section ...')
next() // pass control to the next handler
})
๋ผ์ฐํธ ๊ฒฝ๋ก
๋ผ์ฐํธ ๊ฒฝ๋ก๋, ์์ฒญ ๋ฉ์๋์์ ์กฐํฉ์ ํตํด, ์์ฒญ์ด ์ด๋ฃจ์ด์ง ์ ์๋ ์๋ํฌ์ธํธ๋ฅผ ์ ์ํฉ๋๋ค. ๋ผ์ฐํธ ๊ฒฝ๋ก๋ ๋ฌธ์์ด, ๋ฌธ์์ด ํจํด ๋๋ ์ ๊ท์์ผ ์ ์์ต๋๋ค.
์ฃผ์
In express 5, the characters ?
, +
, *
, []
, and ()
are handled differently than in version 4, please review the migration guide for more information.
์ฃผ์
In express 4, regular expression characters such as $
need to be escaped with a \
.
์ฐธ๊ณ
Express uses path-to-regexp for matching the route paths; see the path-to-regexp documentation for all the possibilities in defining route paths. Express Route Tester๋ ๊ธฐ๋ณธ์ ์ธ Express ๋ผ์ฐํธ์ ํ ์คํธ๋ฅผ ์ํ ํธ๋ฆฌํ ๋๊ตฌ์ด์ง๋ง, ํจํด ์ผ์น๋ ์ง์ํ์ง ์์ต๋๋ค.
๊ฒฝ๊ณ
Query strings are not part of the route path.
๋ฌธ์์ด์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ๋ผ์ฐํธ ๊ฒฝ๋ก์ ๋ช ๊ฐ์ง ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ ์์ฒญ์ ๋ฃจํธ ๋ผ์ฐํธ /
์ ์ผ์น์ํต๋๋ค.
app.get('/', (req, res) => {
res.send('root')
})
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ ์์ฒญ์ /about
์ ์ผ์น์ํต๋๋ค.
app.get('/about', (req, res) => {
res.send('about')
})
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ ์์ฒญ์ /random.text
์ ์ผ์น์ํต๋๋ค.
app.get('/random.text', (req, res) => {
res.send('random.text')
})
๋ฌธ์์ด ํจํด์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ๋ผ์ฐํธ ๊ฒฝ๋ก์ ๋ช ๊ฐ์ง ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ฃผ์
The string patterns in Express 5 no longer work. Please refer to the migration guide for more information.
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ acd
๋ฐ abcd
์ ์ผ์นํฉ๋๋ค.
app.get('/ab?cd', (req, res) => {
res.send('ab?cd')
})
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ abcd
, abbcd
๋ฐ abbbcd
๋ฑ๊ณผ ์ผ์นํฉ๋๋ค.
app.get('/ab+cd', (req, res) => {
res.send('ab+cd')
})
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ abcd
, abxcd
, abRABDOMcd
๋ฐ ab123cd
๋ฑ๊ณผ ์ผ์นํฉ๋๋ค.
app.get('/ab*cd', (req, res) => {
res.send('ab*cd')
})
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ /abe
๋ฐ /abcde
์ ์ผ์นํฉ๋๋ค.
app.get('/ab(cd)?e', (req, res) => {
res.send('ab(cd)?e')
})
์ ๊ท์์ ๊ธฐ๋ฐ์ผ๋ก ํ๋ ๋ผ์ฐํธ ๊ฒฝ๋ก์ ์:
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ ๋ผ์ฐํธ ์ด๋ฆ์ โaโ๊ฐ ํฌํจ๋ ๋ชจ๋ ํญ๋ชฉ๊ณผ ์ผ์นํฉ๋๋ค.
app.get(/a/, (req, res) => {
res.send('/a/')
})
๋ค์์ ๋ผ์ฐํธ ๊ฒฝ๋ก๋ butterfly
๋ฐ dragonfly
์ ์ผ์นํ์ง๋ง, butterflyman
๋ฐ dragonfly man
๋ฑ๊ณผ ์ผ์นํ์ง ์์ต๋๋ค.
app.get(/.*fly$/, (req, res) => {
res.send('/.*fly$/')
})
์กฐํ ๋ฌธ์์ด์ ๋ผ์ฐํธ ๊ฒฝ๋ก์ ์ผ๋ถ๊ฐ ์๋๋๋ค.
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the req.params
object, with the name of the route parameter specified in the path as their respective keys.
Route path: /users/:userId/books/:bookId
Request URL: http://localhost:3000/users/34/books/8989
req.params: { "userId": "34", "bookId": "8989" }
To define routes with route parameters, simply specify the route parameters in the path of the route as shown below.
app.get('/users/:userId/books/:bookId', (req, res) => {
res.send(req.params)
})
The name of route parameters must be made up of โword charactersโ ([A-Za-z0-9_]).
Since the hyphen (-
) and the dot (.
) are interpreted literally, they can be used along with route parameters for useful purposes.
Route path: /flights/:from-:to
Request URL: http://localhost:3000/flights/LAX-SFO
req.params: { "from": "LAX", "to": "SFO" }
Route path: /plantae/:genus.:species
Request URL: http://localhost:3000/plantae/Prunus.persica
req.params: { "genus": "Prunus", "species": "persica" }
์ฃผ์
In express 5, Regexp characters are not supported in route paths, for more information please refer to the migration guide.
To have more control over the exact string that can be matched by a route parameter, you can append a regular expression in parentheses (()
):
Route path: /user/:userId(\d+)
Request URL: http://localhost:3000/user/42
req.params: {"userId": "42"}
๊ฒฝ๊ณ
Because the regular expression is usually part of a literal string, be sure to escape any \
characters with an additional backslash, for example \\d+
.
๊ฒฝ๊ณ
In Express 4.x, the *
character in regular expressions is not interpreted in the usual way. As a workaround, use {0,}
instead of *
. This will likely be fixed in Express 5.
๋ผ์ฐํธ ํธ๋ค๋ฌ
๋ฏธ๋ค์จ์ด์ ๋น์ทํ๊ฒ ์๋ํ๋ ์ฌ๋ฌ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ ๊ณตํ์ฌ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์ ์ผํ ์ฐจ์ด์ ์ ์ด๋ฌํ ์ฝ๋ฐฑ์ next('route')
๋ฅผ ํธ์ถํ์ฌ ๋๋จธ์ง ๋ผ์ฐํธ ์ฝ๋ฐฑ์ ์ฐํํ ์๋ ์๋ค๋ ์ ์
๋๋ค. ์ด๋ฌํ ๋ฉ์ปค๋์ฆ์ ์ด์ฉํ๋ฉด ๋ผ์ฐํธ์ ๋ํ ์ฌ์ ์กฐ๊ฑด์ ์ง์ ํ ํ, ํ์ฌ์ ๋ผ์ฐํธ๋ฅผ ๊ณ์ํ ์ด์ ๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ์ ์ด๋ฅผ ํ์ ๋ผ์ฐํธ์ ์ ๋ฌํ ์ ์์ต๋๋ค.
๋ค์ ์์ ๋ํ๋ ๊ฒ๊ณผ ๊ฐ์ด, ๋ผ์ฐํธ ํธ๋ค๋ฌ๋ ํจ์๋ ํจ์ ๋ฐฐ์ด์ ํํ ๋๋ ๋์ ์กฐํฉํ ํํ์ผ ์ ์์ต๋๋ค.
ํ๋์ ์ฝ๋ฐฑ ํจ์๋ ํ๋์ ๋ผ์ฐํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
app.get('/example/a', (req, res) => {
res.send('Hello from A!')
})
2๊ฐ ์ด์์ ์ฝ๋ฐฑ ํจ์๋ ํ๋์ ๋ผ์ฐํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค(next
์ค๋ธ์ ํธ๋ฅผ ๋ฐ๋์ ์ง์ ํด์ผ ํจ). ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
app.get('/example/b', (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from B!')
})
ํ๋์ ์ฝ๋ฐฑ ํจ์ ๋ฐฐ์ด์ ํ๋์ ๋ผ์ฐํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
const cb2 = function (req, res) {
res.send('Hello from C!')
}
app.get('/example/c', [cb0, cb1, cb2])
๋ ๋ฆฝ์ ์ธ ํจ์์ ํจ์ ๋ฐฐ์ด์ ์กฐํฉ์ ํ๋์ ๋ผ์ฐํธ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
const cb0 = function (req, res, next) {
console.log('CB0')
next()
}
const cb1 = function (req, res, next) {
console.log('CB1')
next()
}
app.get('/example/d', [cb0, cb1], (req, res, next) => {
console.log('the response will be sent by the next function ...')
next()
}, (req, res) => {
res.send('Hello from D!')
})
์๋ต ๋ฉ์๋
๋ค์ ํ์ ํ์๋ ์๋ต ์ค๋ธ์ ํธ์ ๋ํ ๋ฉ์๋(res
)๋ ์๋ต์ ํด๋ผ์ด์ธํธ๋ก ์ ์กํ๊ณ ์์ฒญ-์๋ต ์ฃผ๊ธฐ๋ฅผ ์ข
๋ฃํ ์ ์์ต๋๋ค. ๋ผ์ฐํธ ํธ๋ค๋ฌ๋ก๋ถํฐ ๋ค์ ๋ฉ์๋ ์ค ์ด๋ ํ๋๋ ํธ์ถ๋์ง ์๋ ๊ฒฝ์ฐ, ํด๋ผ์ด์ธํธ ์์ฒญ์ ์ ์ง๋ ์ฑ๋ก ๋ฐฉ์น๋ฉ๋๋ค.
๋ฉ์๋ | ์ค๋ช |
---|---|
res.download() | ํ์ผ์ด ๋ค์ด๋ก๋๋๋๋ก ํ๋กฌํํธํฉ๋๋ค. |
res.end() | ์๋ต ํ๋ก์ธ์ค๋ฅผ ์ข ๋ฃํฉ๋๋ค. |
res.json() | JSON ์๋ต์ ์ ์กํฉ๋๋ค. |
res.jsonp() | JSONP ์ง์์ ํตํด JSON ์๋ต์ ์ ์กํฉ๋๋ค. |
res.redirect() | ์์ฒญ์ ๊ฒฝ๋ก๋ฅผ ์ฌ์ง์ ํฉ๋๋ค. |
res.render() | ๋ณด๊ธฐ ํ ํ๋ฆฌํธ๋ฅผ ๋ ๋๋งํฉ๋๋ค. |
res.send() | ๋ค์ํ ์ ํ์ ์๋ต์ ์ ์กํฉ๋๋ค. |
res.sendFile() | ํ์ผ์ ์ฅํ ์คํธ๋ฆผ์ ํํ๋ก ์ ์กํฉ๋๋ค. |
res.sendStatus() | ์๋ต ์ํ ์ฝ๋๋ฅผ ์ค์ ํ ํ ํด๋น ์ฝ๋๋ฅผ ๋ฌธ์์ด๋ก ํํํ ๋ด์ฉ์ ์๋ต ๋ณธ๋ฌธ์ผ๋ก์ ์ ์กํฉ๋๋ค. |
app.route()
app.route()
๋ฅผ ์ด์ฉํ๋ฉด ๋ผ์ฐํธ ๊ฒฝ๋ก์ ๋ํ์ฌ ์ฒด์ธ ๊ฐ๋ฅํ ๋ผ์ฐํธ ํธ๋ค๋ฌ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค.
๊ฒฝ๋ก๋ ํ ๊ณณ์ ์ง์ ๋์ด ์์ผ๋ฏ๋ก, ๋ชจ๋์ ๋ผ์ฐํธ๋ฅผ ์์ฑํ๋ฉด ์ค๋ณต์ฑ๊ณผ ์คํ๊ฐ ๊ฐ์ํ์ฌ ๋์์ด ๋ฉ๋๋ค. ๋ผ์ฐํธ์ ๋ํ ์์ธํ ์ ๋ณด๋ Router() ๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
app.route()
๋ฅผ ์ฌ์ฉํ์ฌ ์ ์๋ ์ฒด์ธ ๋ผ์ฐํธ ํธ๋ค๋ฌ์ ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
app.route('/book')
.get((req, res) => {
res.send('Get a random book')
})
.post((req, res) => {
res.send('Add a book')
})
.put((req, res) => {
res.send('Update the book')
})
express.Router
express.Router
ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋์ ๋ง์ดํ
๊ฐ๋ฅํ ํธ๋ค๋ฌ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. Router
์ธ์คํด์ค๋ ์์ ํ ๋ฏธ๋ค์จ์ด์ด์ ๋ผ์ฐํ
์์คํ
์ด๋ฉฐ, ๋ฐ๋ผ์ โ๋ฏธ๋ ์ฑ(mini-app)โ์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
๋ค์ ์์์๋ ๋ผ์ฐํฐ๋ฅผ ๋ชจ๋๋ก์ ์์ฑํ๊ณ , ๋ผ์ฐํฐ ๋ชจ๋์์ ๋ฏธ๋ค์จ์ด ํจ์๋ฅผ ๋ก๋ํ๊ณ , ๋ช๋ช ๋ผ์ฐํธ๋ฅผ ์ ์ํ๊ณ , ๊ธฐ๋ณธ ์ฑ์ ํ ๊ฒฝ๋ก์ ๋ผ์ฐํฐ ๋ชจ๋์ ๋ง์ดํธํฉ๋๋ค.
๋ค์์ ๋ด์ฉ์ด ์
๋ ฅ๋ birds.js
๋ผ๋ ์ด๋ฆ์ ๋ผ์ฐํฐ ํ์ผ์ ์ฑ ๋๋ ํ ๋ฆฌ์ ์์ฑํ์ญ์์ค.
const express = require('express')
const router = express.Router()
// middleware that is specific to this router
const timeLog = (req, res, next) => {
console.log('Time: ', Date.now())
next()
}
router.use(timeLog)
// define the home page route
router.get('/', (req, res) => {
res.send('Birds home page')
})
// define the about route
router.get('/about', (req, res) => {
res.send('About birds')
})
module.exports = router
์ดํ ์ฑ ๋ด์์ ๋ค์๊ณผ ๊ฐ์ด ๋ผ์ฐํฐ ๋ชจ๋์ ๋ก๋ํ์ญ์์ค.
const birds = require('./birds')
// ...
app.use('/birds', birds)
์ฑ์ ์ด์ /birds
๋ฐ /birds/about
์ ๋ํ ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๊ฒ ๋์์ผ๋ฉฐ, ํด๋น ๋ผ์ฐํธ์ ๋ํ ํน์ ํ ๋ฏธ๋ค์จ์ด ํจ์์ธ timeLog
๋ฅผ ํธ์ถํ ๊ฒ์
๋๋ค.
But if the parent route /birds
has path parameters, it will not be accessible by default from the sub-routes. To make it accessible, you will need to pass the mergeParams
option to the Router constructor reference.
const router = express.Router({ mergeParams: true })