Express

Route

λΌμš°νŒ…μ€ μš”μ²­ URI 및 νŠΉμ •ν•œ HTTP μš”μ²­ λ©”μ†Œλ“œ (GET, POST, PUT, DELETE) 인 νŠΉμ • μ—”λ“œν¬μΈνŠΈ 에 λŒ€ν•œ ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ— λŒ€ν•΄ μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ‘λ‹΅ν•˜λŠ” 방법을 κ²°μ • ν•˜λŠ”κ²ƒ

Syntax

app.METHOD(PATH, HANDLER)

Parameter

  • app
    • express 의 μΈμŠ€ν„΄μŠ€
  • METHOD
    • HTTP μš”μ²­ λ©”μ†Œλ“œ
  • PATH
    • μ„œλ²„ 경둜
  • HANDLER
    • λΌμš°νŠΈκ°€ μΌμΉ˜ν•  λ•Œ μ‹€ν–‰λ˜λŠ” ν•¨μˆ˜

Example

각 μš”μ²­μ— λ”°λ₯Έ 라우트λ₯Ό μ •μ˜ν•˜κ³  이에 μ‘λ‹΅ν•œλ‹€.

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  res.send('hello world');
});

Method

λŒ€ν‘œμ μΈ Express λΌμš°νŒ… λ©”μ†Œλ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

app.get('/', function (req, res) {
  res.send('Hello World!');
});
app.post('/', function (req, res) {
  res.send('Got a POST request');
});
app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user');
});
app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user');
});

Route Chain

app.route() 을 μ΄μš©ν•˜λ©΄ 라우트 κ²½λ‘œμ— λŒ€ν•œ 체이닝 ν•Έλ“€λŸ¬λ₯Ό μž‘μ„± ν•  수 μžˆλ‹€.

app.route('/book')
  .get(function(req, res) {
    res.send('Get a random book');
  })
  .post(function(req, res) {
    res.send('Add a book');
  })
  .put(function(req, res) {
    res.send('Update the book');
  });

all() λ©”μ†Œλ“œλ₯Ό 톡해 λͺ¨λ“  μ‘λ‹΅μ˜ λŒ€μ‘λ„ κ°€λŠ₯.

app.route('/book')
  .all(function(req, res) {
    res.send('Get a random book');
  });

Route Moduler

λͺ¨λ“ˆμ‹ λ§ˆμš΄νŒ…μ΄ κ°€λŠ₯ν•œ ν•Έλ“€λŸ¬μ˜ μž‘μ„±

var express = require('express');
var router = express.Router();

// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
  console.log('Time: ', Date.now());
  next();
});
// define the home page route
router.get('/', function(req, res) {
  res.send('Birds home page');
});
// define the about route
router.get('/about', function(req, res) {
  res.send('About birds');
});

module.exports = router;
var birds = require('./birds');
...
app.use('/birds', birds);

/birds 및 /birds/about 에 λŒ€ν•œ μš”μ²­μ„ 처리 κ°€λŠ₯ν•˜λ©° ν•΄λ‹Ή λΌμš°νŠΈμ— λŒ€ν•œ νŠΉμ • 미듀웨어 ν•¨μˆ˜μΈ timeLog λ₯Ό 호좜

Express λŠ” HTTP λ©”μ†Œλ“œμ— ν•΄λ‹Ήν•˜λŠ” λ‹€μŒκ³Ό 같은 λΌμš°νŒ… λ©”μ†Œλ“œλ₯Ό μ§€μ›ν•œλ‹€.

get, post, put, head, delete, options, trace, copy, lock, mkcol, move, purge, propfind, proppatch, unlock, report, mkactivity, checkout, merge, m-search, notify, subscribe, unsubscribe, patch, search및 connect.

Path

URI κ²½λ‘œλŠ” λ¬Έμžμ—΄ ν˜Ήμ€ μ •κ·œμ‹μ˜ νŒ¨ν„΄μ—λ„ μ μš©λœλ‹€.

Character Pattern

// acd, abcd
app.get('/ab?cd', function(req, res) {
  res.send('ab?cd');
});
// abcd, abbcd, abbbcd
app.get('/ab+cd', function(req, res) {
  res.send('ab+cd');
});
// abcd, abxcd, abRABDOMcd, ab123cd
app.get('/ab*cd', function(req, res) {
  res.send('ab*cd');
});
// /abe, /abcde
app.get('/ab(cd)?e', function(req, res) {
 res.send('ab(cd)?e');
});

Regular Expression

// a κ°€ ν¬ν•¨λœ λͺ¨λ“  ν•­λͺ©κ³Ό 일치 
app.get(/a/, function(req, res) {
  res.send('/a/');
});
// butterfly, dragonfly κ³ΌλŠ” 일치 
// butterflyman, dragonfly man κ³ΌλŠ” 뢈일치 
app.get(/.*fly$/, function(req, res) {
  res.send('/.*fly$/');
});

Response

응닡 μ˜€λΈŒμ νŠΈμ— λŒ€ν•œ λ©”μ†Œλ“œλŠ” 응닡을 ν΄λΌμ΄μ–ΈνŠΈμ— μ „μ†‘ν•˜κ³  μš”μ²­ 및 μ‘λ‹΅μ˜ μ£ΌκΈ°λ₯Ό μ’…λ£Œν•  수 μžˆλ‹€.
라우트 ν•Έλ“€λŸ¬λ‘œλΆ€ν„° λ‹€μŒ λ©”μ†Œλ“œμ€‘ ν•˜λ‚˜λΌλ„ ν˜ΈμΆœλ˜μ§€ μ•ŠλŠ” 경우 ν΄λΌμ΄μ–ΈνŠΈ μš”μ²­μ€ μ •μ§€λœ μ±„λ‘œ 방치 λœλ‹€.

λ©”μ†Œλ“œ μ„€λͺ…
res.download() 파일이 λ‹€μš΄λ‘œλ“œλ˜λ„λ‘ ν”„λ‘¬ν”„νŠΈν•©λ‹ˆλ‹€.
res.end() 응닡 ν”„λ‘œμ„ΈμŠ€λ₯Ό μ’…λ£Œν•©λ‹ˆλ‹€.
res.json() JSON 응닡을 μ „μ†‘ν•©λ‹ˆλ‹€.
res.jsonp() JSONP 지원을 톡해 JSON 응닡을 μ „μ†‘ν•©λ‹ˆλ‹€.
res.redirect() μš”μ²­μ˜ 경둜λ₯Ό μž¬μ§€μ •ν•©λ‹ˆλ‹€.
res.render() 보기 ν…œν”Œλ¦¬νŠΈλ₯Ό λ Œλ”λ§ν•©λ‹ˆλ‹€.
res.send() λ‹€μ–‘ν•œ μœ ν˜•μ˜ 응닡을 μ „μ†‘ν•©λ‹ˆλ‹€.
res.sendFile νŒŒμΌμ„ μ˜₯ν…Ÿ 슀트림의 ν˜•νƒœλ‘œ μ „μ†‘ν•©λ‹ˆλ‹€.
res.sendStatus() 응닡 μƒνƒœ μ½”λ“œλ₯Ό μ„€μ •ν•œ ν›„ ν•΄λ‹Ή μ½”λ“œλ₯Ό λ¬Έμžμ—΄λ‘œ ν‘œν˜„ν•œ λ‚΄μš©μ„ 응닡 λ³Έλ¬ΈμœΌλ‘œμ„œ μ „μ†‘ν•©λ‹ˆλ‹€.
const router = express.Router();

// ν΄λΌμ΄μ–ΈνŠΈκ°€ 루트 경둜둜 μ§„μž…ν•˜λ©΄ /index 경둜둜 λ¦¬λ‹€μ΄λ ‰νŠΈ μ‹œν‚¨λ‹€.
router.all('/', (req, res) => {
  res.redirect('/index');
});

// λͺ¨λ“  경둜λ₯Ό index 둜 λžœλ”λ§ ν•œλ‹€.
router.all('*', (req, res) => {
  res.render('index');
});