๐ŸŒณ๋ชฉํ‘œ

Response ๋ชจ๋“ˆ์„ ๋งŒ๋“  ๊ฒƒ์ฒ˜๋Ÿผ ์ต์Šคํ”„๋ ˆ์Šค์™€ ์œ ์‚ฌํ•œ ์š”์ฒญ ๊ฐ์ฒด์ธ Request ๋ชจ๋“ˆ์„ ๋งŒ๋“ญ๋‹ˆ๋‹ค.

์ฟผ๋ฆฌ์ŠคํŠธ๋ง ์š”์ฒญ

๋จผ์ € ๋ธŒ๋žœ์น˜๋ฅผ ์ด๋™ํ•ด ๋ณผ๊นŒ์š”?

$ git checkout -f request/spec

์„œ๋ฒ„๋ฅผ ์žฌ ๊ตฌ๋™ํ•˜๊ณ  ๋ธŒ๋ผ์šฐ์ ธ๋กœ ์ ‘์†ํ•ด ๋ณด์„ธ์š”.

ํฌ๋กฌ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋กœ ๋ณด๋‹ˆ ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜ค๋Š”๋ฐ์š”. ์ด์ „๊ณผ ๋‹ค๋ฅด๊ฒŒ /api/posts ์š”์ฒญ์— ์ฟผ๋ฆฌ๋ฌธ์ž์—ด์ด ์ถ”๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

?limit=2&page=1

๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒŒ 404๋กœ ์‘๋‹ต๋˜๋Š”๊ตฐ์š”. ๋ถ„๋ช…ํžˆ /api/posts ๋ผ์šฐํŠธ๋ฅผ ๋“ฑ๋กํ–ˆ๋Š”๋ฐ ๋ง์ด์ฃ .

์•„๋งˆ๋„ ์„œ๋ฒ„์—์„œ ๋ฏธ๋“ค์›จ์–ด ๋“ฑ๋ก์‹œ ์‚ฌ์šฉํ•œ URL๊ณผ ์š”์ฒญํ•œ URL์ด โ€˜์ •ํ™•ํžˆโ€™ ์ผ์น˜ํ•˜์ง€ ์•Š๋Š”๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. src/Middleware.js์— ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”?

if (nextMw._path) {
  const pathMatched = _req.url === nextMw._path; // ๋ฐ”๋กœ ์ด ๋ถ€๋ถ„ !!!
  return pathMatched ? nextMw(_req, _res, next) : _run(i + 1)
}

๋“ฑ๋กํ•œ ๊ฒฝ๋กœ(nextMw._path)์™€ ์š”์ฒญํ•œ ์ „์ฒด ์ฃผ์†Œ(_req.url)๊ฐ€ ์™„๋ฒฝํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š๊ตฐ์š”. ๊ทธ๋ž˜์„œ ๋“ฑ๋กํ•œ ๋ฏธ๋“ค์›จ์–ด๋„ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๊ณ ์š”. ๊ฒฐ๊ตญ์— 404๋ฅผ ์‘๋‹ตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์„ ์ œ์™ธํ•œ ์ •ํ™•ํ•œ ๊ฒฝ๋กœ์„ ๋ฝ‘์•„์„œ ๋น„๊ตํ•˜๋Š” ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

Request ๋ชจ๋“ˆ

HTTP ์—์„œ ์š”์ฒญ URL์€ ๋‹ค์Œ ํ˜•์‹์„ ๊ฐ–์Šต๋‹ˆ๋‹ค.

Protocol + Domain + Path + QueryString + Body

์ด ์ค‘ ์šฐ๋ฆฌ๊ฐ€ ์‚ฌ์šฉํ•  ๊ฒƒ์€ path, querystrig, body ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

์ง€๊ธˆ๊นŒ์ง€ ๊ฒฝ๋กœ ๋งค์นญ์— ์‚ฌ์šฉํ•œ req.url์€ path์™€ querystring์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ๋ฌธ์ œ์˜ ์›์ธ์ด๊ธฐ๋„ ํ•˜๊ตฌ์š”.

req๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์˜ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•œ๋‹ค๋ฉด ์–ด๋–จ๊นŒ์š”?

req.path = "/api/posts"
req.query = {
  limit: "2",
  page: "1"
}

๊ฒฝ๋กœ๋ฅผ ๋น„๊ตํ• ๋•Œ req.url ๋Œ€์‹ ์— req.path๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ •ํ™•ํ•œ ๋น„๊ต๊ฐ€ ๋˜๊ฒ ์ฃ ? ๊ฒŒ๋‹ค๊ฐ€ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์— ์ ‘๊ทผํ•  ๋•Œ๋Š” ๋ฏธ๋ฆฌ ํŒŒ์‹ฑ๋œ req.query ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ limit๊ณผ page ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ req ๊ฐ์ฒด๋ฅผ ํ™•์žฅํ•˜๋Š” Request ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด ๋ณผ๊นŒ์š”? ๋„ค ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ  ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

require('should');
const Request = require('./Request');

describe('Request', () => {
  it('์ƒ์„ฑ ์ธ์ž๊ฐ€ ์—†์œผ๋ฉด ์—๋Ÿฌ๋ฅผ ๋˜์ง„๋‹ค', () => {
    should(() => Request()).throw()
  })

ํ…Œ์ŠคํŠธ ๋ชจ๋“ˆ์„ ์ž„ํฌํŠธํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ๋ชจ๋“ˆ ์ƒ์„ฑ์‹œ ์ธ์ž๊ฐ€ ์—†์œผ๋ฉด ์—๋Ÿฌ๋ฅผ ๋˜์ง€๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ํ™•์žฅํ•  req ์ธ์ž๊ฐ€ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ˆ๊นŒ์š”.

  describe('๋ฐ˜ํ™˜ ๊ฐ์ฒด', () => {
    let req, path, qs

    beforeEach(() => {
      path = '/api/posts'
      qs = {
        limit: '2',
        page: '1'
      }
      const encodedQs = `limit=${qs.limit}&page=${qs.page}`
      req = Request({url: `${path}?${encodedQs}`})
    })

๋‘ ๋ฒˆ์งธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ถ€ํ„ฐ๋Š” ์ƒ์„ฑ ์ธ์ž๊ฐ€ ์˜ฌ๋ฐ”๋กœ ๋“ค์–ด๊ฐ„ ์ดํ›„์˜ ํ…Œ์ŠคํŠธ ์ž…๋‹ˆ๋‹ค. ๋ชจ๋“ˆ ์ƒ์„ฑ ์ฝ”๋“œ๋Š” ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ์ค‘๋ณต์œผ๋กœ ์‚ฌ์šฉ๋  ๊ฒƒ์ด๋ฏ€๋กœ beforeEach()์—์„œ ๊ธฐ์ˆ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๊ธฐ์กด req ๊ฐ์ฒด์˜ url ๋ฌธ์ž์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ path์™€ querystring์„ ์ถ”์ถœํ•  ๊ฒƒ์ด๋ฏ€๋กœ url ๋ฌธ์ž์—ด์„ ํ‚ค๋กœ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    it('path ์†์„ฑ์„ ๋…ธ์ถœํ•œ๋‹ค', () => {
      req.should.have.property('path', path)
    })

์ƒ์„ฑ๋œ ๊ฐ์ฒด๊ฐ€ path ์†์„ฑ์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. path์—๋Š” querystring์ด ์ œ๊ฑฐ๋œ ์ˆœ์ˆ˜ํ•œ ๊ฒฝ๋กœ๋งŒ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    it('query ์†์„ฑ์„ ๋…ธ์ถœํ•œ๋‹ค', () => {
      req.should.have.property('query')
      req.query.limit.should.be.equal(qs.limit)
      req.query.page.should.be.equal(qs.page)
    })
  })
})

query ๊ฐ์ฒด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ฐ์ฒด์— limit๊ณผ page ๊ฐ’์ด ์˜ฌ๋ฐ”๋กœ ํ• ๋‹น๋˜์–ด ์žˆ๋Š”์ง€๋„ ์ฒดํฌํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

๐Ÿค์‹ค์Šต - Request ๋ชจ๋“ˆ์„ ๊ตฌํ˜„ํ•ด ๋ณด์„ธ์š”

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๊ธฐ์กด req๊ฐ์ฒด์— path์™€ query ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” Request ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด ๋ณด์„ธ์š”.

ํžŒํŠธ: ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆŒ ๋•Œ๋Š” String.prorotype.split()

๐Ÿคํ’€์ด

์ด ์‹ค์Šต์—์„œ๋Š” split() ์ŠคํŠธ๋ง ๋ฉ”์†Œ๋“œ๋งŒ ์ž˜ ์‚ฌ์šฉํ•ด๋„ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆ„์–ด ํ’€์–ด๋ณผ๊ฒŒ์š”.

const Request = req => {
  if (!req) throw Error('req is required')
  
  // req ํ™•์žฅ ์ฝ”๋“œ

  return req
}

module.exports = Request

์ƒ์„ฑ์ธ์ž req๊ฐ€ ์—†์œผ๋ฉด ์—๋Ÿฌ๋ฅผ ๋˜์ ธ ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ํ™•์žฅ๋œ req ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

  const partials = req.url.split('?')

  const path = partials[0] || '/';
  req.path = req.path || path;

  if (!partials[1] || !partials[1].trim()) return req;

split() ๋ฉ”์†Œ๋“œ๋Š” ํŠน์ • ์บ๋ฆญํ„ฐ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž๋ฅผ ์ชผ๊ฒ ๋’ค ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. req.url์„ โ€œ?โ€๋กœ ๋ถ„๋ฆฌํ•œ ๋’ค partials ๋ฐฐ์—ด๋กœ ๋‹ด์Šต๋‹ˆ๋‹ค.

์ด๋ ‡๊ฒŒ ๋ถ„๋ฆฌ๋œ partials์˜ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์—๋Š” ๊ฒฝ๋กœ ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ์—” ๊ธฐ๋ณธ ๊ฒฝ๋กœ โ€œ/โ€๋ฅผ ํ• ๋‹นํ•˜๊ตฌ์š”. ์ด path์„ req.path์— ์ €์žฅํ•ด์„œ req ๊ฐ์ฒด๋ฅผ ํ™•์žฅํ•ฉ๋‹ˆ๋‹ค.

๋ถ„๋ฆฌ๋œ ๋ฌธ์ž์—ด์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์ด ์—†์„ ๊ฒฝ์šฐ์—” ๋ฐ”๋กœ req ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.

  const qs = partials[1].split('&').reduce((obj, p) => {
    const frag = p.split('=')
    obj[frag[0]] = frag[1]
    return obj
  }, {})

  req.query = qs

์ฟผ๋ฆฌ์ŠคํŠธ๋ง์€ โ€œ&โ€ ๋ฌธ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ โ€œkey=valueโ€ ํ˜•์‹์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋จผ์ € โ€œ&โ€๋กœ ๋ฌธ์ž๋ฅผ ๋ถ„๋ฆฌํ•œ ๊ฒƒ์ด์ฃ .

์ด๋ ‡๊ฒŒ ๋ฐ˜ํ™˜๋œ ๋ฐฐ์—ด์„ reduce ํ•จ์ˆ˜๋กœ ๋Œ๋ฆฌ๋ฉด์„œ obj์— ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์Šต๋‹ˆ๋‹ค. ๋ฐฐ์—ด์˜ ์š”์†Œ(p)๋Š” โ€œkey=valueโ€ ํ˜•ํƒœ์˜ ๋ฌธ์ž์—ด์ผ ํ…Œ๋‹ˆ๊น โ€œ=โ€ ๋ฌธ์ž ๊ธฐ์ค€์œผ๋กœ ํ•œ ๋ฒˆ ๋” ๋ถ„๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋ฉด frag[0]์—๋Š” ํ‚ค๊ฐ€ frag[1]์—๋Š” ๋ฐธ๋ฅ˜๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ฒ ์ง€์š”.

์ด๋ ‡๊ฒŒ ํ•ด์„œ ์™„์„ฑ๋œ qs ๊ฐ์ฒด๋ฅผ req.query๋กœ ํ• ๋‹นํ•˜์—ฌ req ๊ฐ์ฒด๋ฅผ ํ™•์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Request ๋ชจ๋“ˆ์„ Application์—์„œ ์‚ฌ์šฉ

Reponse๋ฅผ Application์—์„œ ์‚ฌ์šฉํ•œ ๊ฒƒ์ฒ˜๋Ÿผ Request๋„ ๊ทธ๋ ‡๊ฒŒ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. src/Application.js ์ฝ”๋“œ์ค‘ req ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ณผ๊นŒ์š”?

// ...
const Request = require('./Request');

const Application = () => {
  const _server = http.createServer((req, res) => {
    _middleware.run(Request(req), Response(res)) // Request ๊ฐ์ฒด๋กœ ๊ต์ฒด 
  });

๊ธฐ์กด์˜ req ๊ฐ์ฒด๋ฅผ Request ๊ฐ์ฒด๋กœ ํ™•์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Request ๋ชจ๋“ˆ์„ Middleware์—์„œ ์‚ฌ์šฉ

์ดˆ๋ฐ˜์— ์–ธ๊ธ‰ํ•œ ๋ฌธ์ œ์˜ ์›์ธ์„ ๋‹ค์‹œ ๋– ์˜ฌ๋ ค ๋ณผ๊นŒ์š”? ๋“ฑ๋กํ–ˆ๋˜ API์ธ๋ฐ 404์‘๋‹ต์„ ๋ฐ›์€ ๋ฌธ์ œ์˜€์ง€์š”? ๊ทธ๊ฒƒ์€ ๋ฐ”๋กœ ๋“ฑ๋กํ•œ ๊ฒฝ๋กœ์™€ ์š”์ฒญ ์ฃผ์†Œ๊ฐ€ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ถ€๋ถ„์˜ ์ฝ”๋“œ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” src/Middleware.js ์ฝ”๋“œ๋ฅผ ๊ฐœ์„ ํ•ด ๋ณด์ง€์š”.

     if (nextMw._path) {
      const pathMatched = _req.path === nextMw._path // ๊ฒฝ๋กœ๋งŒ ์ฒดํฌ 
      return pathMatched ? nextMw(_req, _res, next) : _run(i + 1)
     }

์ด์ „์—๋Š” ์ฟผ๋ฆฌ๋ฌธ์ž์—ด์ด ํฌํ•จ๋œ _req.url์„ ๋น„๊ตํ•˜๋Š” ์ฝ”๋“œ์˜€์Šต๋‹ˆ๋‹ค. ์ด์ œ๋Š” ๊ฒฝ๋กœ๋งŒ ์ €์žฅ๋œ _req.path๋ฅผ ๋น„๊ตํ•ด์„œ ์š”์ฒญ์ •๋ณด์™€ ์ •ํ™•ํ•œ ๋น„๊ต๋ฅผ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์„œ๋ฒ„๋ฅผ ๋‹ค์‹œ ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด ๋ณผ๊นŒ์š”?

ํฌ์ŠคํŠธ ๋ชฉ๋ก์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ๋ณด๋‹ˆ API๊ฐ€ ์ž˜ ์‘๋‹ต๋˜๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”.

ํฌ๋กฌ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ๋ณด๋ฉด ์„ฑ๊ณต์„ ์˜๋ฏธํ•˜๋Š” 200 ์ƒํƒœ์ฝ”๋“œ๊ฐ€ ์‘๋‹ต๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์š”์ฒญ ์ฃผ์†Œ์˜ ์ฟผ๋ฆฌ๋ฌธ์ž์—ด์„ ์œ ์‹ฌํžˆ ์‚ดํŽด ๋ณด์„ธ์š”.

?limit=2&page=1

ํฌ์ŠคํŠธ ๊ฐฏ์ˆ˜๋ฅผ ์ตœ๋Œ€ 2๊ฐœ๋กœ ํ•œ์ •ํ•˜๊ณ  1ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ–ˆ์œผ๋‹ˆ๊น ์ฒ˜์Œ 2๊ฐœ ํฌ์ŠคํŠธ๋งŒ ๋‚˜์˜ค๋Š”๊ฒŒ ๋งž์„ ๊ฒ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์ง€๊ธˆ์€ ํฌ์ŠคํŠธ 3๊ฐœ๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ์ฃ .

๐Ÿค์‹ค์Šต - Request ๋ชจ๋“ˆ์„ ์ด์šฉํ•ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด ๋ณด์„ธ์š”

/api/posts API์—์„œ ํŽ˜์ด์ง€๋„ค์ด์…˜ ๋กœ์ง์„ ์ถ”๊ฐ€๋กœ ๊ตฌํ˜„ํ•˜์„ธ์š”. ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์•„๋ž˜ ๋ธŒ๋žœ์น˜๋กœ ์ด๋™ํ•˜์‹œ๊ตฌ์š”.

$ git checkout -f request/props

๐Ÿคํ’€์ด

๊ฐ„๋‹จํžˆ ํ’€์–ด ๋ณด์ฃ .

ํฌ์ŠคํŠธ ์กฐํšŒ API๊ฐ€ ์žˆ๋Š” routes/api/posts.js ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

const index = () => (req, res, next) => {
  const limit = req.query.limit * 1 || 2
  const page = req.query.page * 1 || 1

  const begin = (page - 1) * limit
  const end = begin + limit

  res.json(posts.slice(begin, end))
 }

req.query์— ๋‹ด๊ฒจ์žˆ๋Š” ์ฟผ๋ฆฌ๋ฌธ์ž์—ด ์ •๋ณด๋Š” ๋ชจ๋‘ ๋ฌธ์ž์—ด๋กœ ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฑธ ๋ชจ๋‘ ์ˆซ์žํ˜•์œผ๋กœ ๋ณ€ํ™˜(*1) ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์—†์„ ๊ฒฝ์šฐ์—๋Š” limit์€ 2, page๋Š” 1๋กœ ๊ฐ ๊ฐ ๊ธฐ๋ณธ๊ฐ’์„ ํ• ๋‹นํ–ˆ๊ตฌ์š”

๊ทธ๋ฆฌ๊ณ ๋‚˜์„œ posts ๋ฐฐ์—ด์—์„œ ์š”์ฒญํ•œ ๋ฐ์ดํ„ฐ ๋ฒ”์œ„๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด begin๊ณผ end๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ตฌ๊ฐ„์˜ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•œ ๋ฐฐ์—ด์„ ์ œ์ด์Šจ์œผ๋กœ ์‘๋‹ตํ•ฉ๋‹ˆ๋‹ค.

์„œ๋ฒ„๋ฅผ ์žฌ์‹คํ–‰ํ•˜๊ณ  ํ™•์ธํ•ด ๋ณผ๊นŒ์š”?

3๊ฐœ ํฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•œ ์ด์ „๊ณผ ๋‹ฌ๋ฆฌ ์š”์ฒญํ•œ 2๊ฐœ ํฌ์ŠคํŠธ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

Request ๋ชจ๋“ˆ๊นŒ์ง€ ํ•ด์„œ ์ต์Šคํ”„๋ ˆ์ŠคJS ์„น์…˜์„ ๋ชจ๋‘ ์ฑ„์› ์Šต๋‹ˆ๋‹ค.

์ •๋ฆฌ

  • ์š”์ฒญ ์ •๋ณด์— ์‰ฝ๊ฒŒ ์ ‘๊ทผํ•˜๊ฒŒ ์œ„ํ•œ Request ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
  • req.path, req.query

๋ชฉ์ฐจ ๋ฐ”๋กœ๊ฐ€๊ธฐ