โ ์ค์ํ๋ ๊ฒ์ ์ธ๊ฐ์ ์ด๋ค. ๊ทธ๋ฆฌ๊ณ ๊ทธ ์ค์๋ฅผ ๋๊ณ ์ปดํจํฐ ํ์ ํ๋ ๊ฒ์ ๋ ์ธ๊ฐ์ ์ด๋ค. โ
- Robert Orben

์๋ต ๊ฐ์ฒด - response
- res.app: ๋๊ฐ์ด res ๊ฐ์ฒด๋ฅผ ํตํด app ๊ฐ์ฒด์ ์ ๊ทผํ๋ค. res.app.get('')๊ฐ์ด ์ฌ์ฉ ๊ฐ๋ฅ.
- res.set(ํค๋, ๊ฐ) / res.setHeader(ํค๋, ๊ฐ): ์๋ต์ ํค๋๋ฅผ ์ค์ ํ๋ค. req.get()์ด ํค๋๊ฐ์ ๊ฐ์ ธ์ค๋๊ฑฐ๋ผ๋ฉด ์ด๊ฑด ํค๋ ์ค์
- res.status(์ฝ๋) / res.sendStatus(์ฝ๋): ์๋ต ์์ HTTP ์ํ ์ฝ๋๋ฅผ ์ง์ ํ๋ค.
- res.type(type) : Contents-Type ํค๋๋ฅผ ์ค์ ํ ์ ์๋ ๊ฐ๋จํ ๋ฉ์๋.
- res.cookie(ํค, ๊ฐ, ์ต์ ): ์ฟ ํค๋ฅผ ์๋ต์ ์ค์ ํ๋ ๋ฉ์๋์ด๋ค. (cookie-parser ํจํค์ง๊ฐ ํ์)
- res.clearCookie(ํค, ๊ฐ, ์ต์
): ์ฟ ํค๋ฅผ ์๋ต์์ ์ ๊ฑฐํ๋ ๋ฉ์๋์ด๋ค.
- res.end(): ๋ฐ์ดํฐ ์์ด ์๋ต์ ๋ณด๋ธ๋ค.
- res.json(JSON): JSON ํ์์ ์๋ต์ ๋ณด๋ธ๋ค.
- res.redirect(์ฃผ์): ๋ฆฌ๋ค์ด๋ ํธํ ์ฃผ์์ ํจ๊ป ์๋ต์ ๋ณด๋ธ๋ค.
- res.locals / res.render(๋ทฐ, ๋ฐ์ดํฐ):
res.locals๋ ๋ทฐ๋ฅผ ๋ ๋๋งํ๋ ๊ธฐ๋ณธ ์ฝํ ์คํธ๋ฅผ ํฌํจํ๋ ๊ฐ์ฒด๋ค.
res.render๋ jade์ ๊ฐ์ ํ ํ๋ฆฟ ์์ง์ ์ฌ์ฉํ์ฌ ๋ทฐ๋ฅผ ๋ ๋๋งํ๋ค. - res.send(body), res.send(status, body) : ํด๋ผ์ด์ธํธ์ ์๋ต์ ๋ณด๋. ์ํ ์ฝ๋๋ ์ต์
.
๊ธฐ๋ณธ ์ฝํ ์ธ ํ์ ์ text/html์ด๋ฏ๋ก text/plain์ ๋ณด๋ด๋ ค๋ฉด res.set(โContent-Typeโ, โtext/plainโ)์ ๋จผ์ ํธ์ถ ํด์ผํ๋ค. - res.sendFile(๊ฒฝ๋ก): ๊ฒฝ๋ก์ ์์นํ ํ์ผ์ ์๋ตํ๋ค.
- res.attachment([filename]), res.download(path, [filename], [callback]) : ํด๋ผ์ด์ธํธ์๊ฒ ํ์ผ์ ํ์ํ์ง ๋ง๊ณ ๋ค์ด๋ก๋ ๋ฐ์ผ๋ผ๊ณ ์ ์กํจ. filename์ ์ฃผ๋ฉด ํ์ผ ์ด๋ฆ์ด ๋ช ์๋๋ฉฐ, res.attachment๋ ํค๋๋ง ์ค์ ํ๋ฏ๋ก ๋ค์ด๋ก๋๋ฅผ ์ํ node ์ฝ๋๊ฐ ๋ฐ๋ก ํ์ํ๋ค.
res.json(JSON)
// ์ด ๋ถ๋ถ์ ํ๋๋ก ์งง๊ฒ ํฉ์น express๋ฉ์๋
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringfy({hello: 'nomad'}));
// โ
res.json({hello: 'nomad'})โโ
res.send([body])
res.send(Buffer.from('whoop'))
res.send({ some: 'json' }) // res.status(200).send({ some: 'json' }) ์ ๊ฐ๋ค. 200์ด ์๋ต๋์ด์์
res.send('<p>some html</p>')
res.status(404).send('Sorry, we cannot find that!')
res.status(500).send({ error: 'something blew up' })
res.status(code)
// http๋ชจ๋์์ ๋์ค์ด ์ฝ๋๊ฐ ํ๋๋ก ์ค์ด๋ค์๋ค๊ณ ๋ณด๋ฉด ๋๋ค.
// res.writeHead(200, { 'Content-Type', 'text/html'});
// res.end("์๋
ํ์ธ์");
res.status(403).end()
res.status(400).send('Bad Request')
res.status(404).sendFile('/absolute/path/to/404.png')
res.sendStatus(statusCode)
res.sendStatus(200) // == res.status(200).send('OK')
res.sendStatus(403) // == res.status(403).send('Forbidden')
res.sendStatus(404) // == res.status(404).send('Not Found')
res.sendStatus(500) // == res.status(500).send('Internal Server Error')
res.redirect([status,] path)
// ๊ธฐ์กด http๋ชจ๋ ์ฝ๋
res.writeHead(301, {
Location: 'http://example.com',
// 'Set-Cookie': '~',
});
res.end();
// ์ต์คํ๋ ์ค ์ฝ๋
res.redirect(301, 'http://example.com') // 301๋ก ํด๋นํ์ด์ง๋ก ๊ฐ์ ์ด๋
res.redirect('/foo/bar')
res.redirect('http://example.com')
res.redirect('../login')
res.render(view, [, locals][, callback])
// send the rendered view to the client
res.render('index')
// if a callback is specified, the rendered HTML string has to be sent explicitly
res.render('index', function (err, html) {
res.send(html)
})
// pass a local variable to the view
res.render('user', { name: 'Tobi' }, function (err, html) {
// ...
})
res.end()
res.json(JSON)
res.redirect(์ฃผ์)
res.render(๋ทฐ, ๋ฐ์ดํฐ)
res.send(๋ฐ์ดํฐ)
res.sendFile(๊ฒฝ๋ก)
๋ ๊ฐ ๋ผ์ฐํฐ์ ๋ฐ๋์ ํ๋ฒ๋ง ์จ์ผ๋๋๊ฑด ์์ง๋ง์.

์์ฒญ ๊ฐ์ฒด - request
- req.app: req ๊ฐ์ฒด๋ฅผ ํตํ app ๊ฐ์ฒด๋ก์ ์ ๊ทผ์ด๋ค. ์๋ฅผ ๋ค์ด req.app.get('port')์ ๊ฐ์ ์์ผ๋ก ์ฌ์ฉํ ์ ์๋ค.
- req.cookies: cookie-parser ๋ฏธ๋ค์จ์ด๊ฐ ๋ง๋๋ ์์ฒญ์ ์ฟ ํค๋ฅผ ํด์ํ ๊ฐ์ฒด์ด๋ค.
- req.signedCookies: ์๋ช ๋ ์ฟ ํค๋ค์ req.cookies ๋์ ์ฌ๊ธฐ์ ๋ด๊ธด๋ค.
- req.get(ํค๋ ์ด๋ฆ): ํค๋์ ๊ฐ์ ๊ฐ์ ธ์จ๋ค. req.get('Content-type')
- req.body: body-parser ๋ฏธ๋ค์จ์ด๊ฐ ๋ง๋๋ ์์ฒญ์ ๋ณธ๋ฌธ์ ํด์ํ ๊ฐ์ฒด์ด๋ค. POST ๋ฐฉ์์ผ๋ก ๋์ด์ค๋ ๋ฐ์ดํฐ๋ฅผ ๋ด๋๋ค.
- req.params: ๋ผ์ฐํธ ๋งค๊ฐ๋ณ์์ ๋ํ ์ ๋ณด๊ฐ ๋ด๊ธด๋ค.
- req.query: GET๋ฐฉ์์ผ๋ก ๋์ด์ค๋ ๋ฐ์ดํฐ, ์ฟผ๋ฆฌ์คํธ๋ง์ ์ ๋ณด๊ฐ ๋ด๊ธด๋ค.
- req.route : ํ์ฌ ๋ผ์ฐํธ์ ๊ดํ ์ ๋ณด. ๋๋ฒ๊น ์ฉ.
- req.headers : HTTP์ Header ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
- req.accepts([types]) : ํด๋ผ์ด์ธํธ๊ฐ ํด๋นํ๋ ํ์ ์ ๋ฐ์ ์ ์๋์ง ํ์ธํ๋ ๊ฐ๋จํ ๋ฉ์๋.
- req.ip: ์์ฒญ์ ip ์ฃผ์๋ฅผ ๋ด๋๋ค.
- req.path : ํด๋ผ์ด์ธํธ๊ฐ ์์ฒญํ ๊ฒฝ๋ก. ํ๋กํ ์ฝ, ํธ์คํธ, ํฌํธ, ์ฟผ๋ฆฌ์คํธ๋ง์ ์ ์ธํ ์์ ์์ฒญ ๊ฒฝ๋ก๋ค.
- req.host : ์์ฒญ ํธ์คํธ ์ด๋ฆ์ ๋ฐํํ๋ ๊ฐ๋จํ ๋ฉ์๋. ์กฐ์๋ ์ ์์ผ๋ฏ๋ก ๋ณด์ ๋ชฉ์ ์ผ๋ก๋ ์ฌ์ฉํ๋ฉด ์๋๋ค.
- req.xhr : ์์ฒญ์ด ajax ํธ์ถ๋ก ์์๋์๋ค๋ฉด true๋ฅผ ๋ฐํํ๋ ํ๋กํผํฐ
- req.protocol : ํ์ฌ ์์ฒญ์ ํ๋กํ ์ฝ (http, https ๋ฑ)
- req.secure : ํ์ฌ ์์ฒญ์ด ๋ณด์ ์์ฒญ(SSL?) ์ด๋ฉด true๋ฅผ ๋ฐํ
- req.url (req.originalUrl) : URL ๊ฒฝ๋ก์ ์ฟผ๋ฆฌ ์คํธ๋ง์ ๋ฐํ. ์๋ณธ ์์ฒญ์ loggingํ๋ ๋ชฉ์ ์ผ๋ก ๋ง์ด ์ฐ์.
- req.acceptedLanguages : ํด๋ผ์ด์ธํธ๊ฐ ์ ํธํ๋ ์์ฐ์ด ๋ชฉ๋ก์ ๋ฐํ. header์์ ํ์ฑํ๋ฉด ๋ค๊ตญ์ด๋ฅผ ์ง์ํ๋ ์ดํ๋ฆฌ์ผ์ด์ ์ด๋ผ๋ฉด ์ด๊ธฐ ์ธ์ด ์ ํ์ ๋์์ ์ค ์ ์์.
req.query
// GET /search?q=tobi+ferret
console.dir(req.query.q)
// => 'tobi ferret'
// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
console.dir(req.query.order)
// => 'desc'
console.dir(req.query.shoe.color)
// => 'blue'
console.dir(req.query.shoe.type)
// => 'converse'
// GET /shoes?color[]=blue&color[]=black&color[]=red
console.dir(req.query.color)
// => ['blue', 'black', 'red']
req.body
var express = require('express')
var app = express()
app.use(express.json()) // for parsing application/json
app.use(express.urlencoded({ extended: true })) // for parsing application/x-www-form-urlencoded
app.post('/profile', function (req, res, next) {
console.log(req.body)
res.json(req.body)
})
ํด๋ผ์ด์ธํธ์์ ์์ฒญํ ๋ GET ๋ฐฉ์์ผ๋ก ์์ฒญํ ์ง, ์๋๋ฉด POST ๋ฐฉ์์ผ๋ก ์์ฒญํ ์ง ๋ชจ๋ฅด๋ ๊ฒฝ์ฐ๊ฐ ์์ ์๋ ์๋ค.
์ด๋ด ๋๋ ๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ ๊ฐ์ง ์์ฒญ ํ๋ผ๋ฏธํฐ๋ฅผ ๋ชจ๋ ๊ฒ์ฌํ ์ ์๋ค.
const paramId = req.body.id || req.query.id;
req.get(field), req.header(name)
req.get('Content-Type')
// => "text/plain"
req.get('content-type')
// => "text/plain"
req.get('Something')
// => undefined
์ด ๊ธ์ด ์ข์ผ์ จ๋ค๋ฉด ๊ตฌ๋ & ์ข์์
์ฌ๋ฌ๋ถ์ ๊ตฌ๋
๊ณผ ์ข์์๋
์ ์์๊ฒ ํฐ ํ์ด ๋ฉ๋๋ค.