TIL

23/11/24 TIL __ ejs๋ฅผ ์ด์šฉํ•ด express ํ™˜๊ฒฝ์—์„œ ์„ค๊ณ„๋œ ๋‚ด์šฉ์„ ๋ณด์—ฌ์ฃผ์ž 2

GABOJOK 2023. 11. 25. 01:16

 

 

๐Ÿž๏ธ   ์ƒํ™ฉ

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•  ๋•Œ์— ejs๋ฅผ ํ™œ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š”๋ฐ ์„ฑ๊ณตํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„๋•Œ, ํŒŒ๋žŒ์Šค๋กœ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ฉด์„œ router๋ฅผ ๋™์ž‘์‹œํ‚ค๋Š” ๋ฐ์—๋„ ์„ฑ๊ณตํ–ˆ๋‹ค.

๊ทผ๋ฐ ๋ฌธ์ œ๋Š” post๋‚˜ get ์š”์ฒญ์ด ์•„๋‹Œ ์ด์™ธ์˜ ์š”์ฒญ์ด์˜€๋‹ค.

๊ทธ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์ฐพ์•„๋ณด์•˜๋‹ค.

 

 

๐Ÿ“ท  ejs ๋กœ delete ์š”์ฒญ , put , patch ์š”์ฒญํ•˜๊ธฐ. 

 

form ํƒœ๊ทธ๋กœ ์ „์†กํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ, 

๋ฌธ์ œ๋Š” formํƒœ๊ทธ๋Š”  put ์š”์ฒญ์ด๋‚˜ patch ์š”์ฒญ, delete ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์—ˆ๋‹ค.

์•„๋ฌด๋ฆฌ ์ ์–ด๋„ ๊ณ„์† ์ž‘๋™ํ•˜์ง€ ์•Š์•„์„œ ๋‹นํ™ฉํ–‡๋Š”๋ฐ, ์›๋ž˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†๋Š”๊ฑธ ์ ๊ณ  ์žˆ์—ˆ๋˜ ๊ฒƒ.!!

 

 

 

์ด์—๋Œ€ํ•œ ๋ฐฉ๋ฒ•์„ ์ฐพ๋‹ค๋ณด๋‹ˆ ๊ผผ์ˆ˜๋ฅผ ๋ฐœ๊ฒฌํ–ˆ๋‹ค. 

  <form action="/api/user/<%= userId %>" method="POST">
    <input type = 'hidden' name="_method" value="DELETE" onclick="location.href = '/api/user/<%=userId%>'">์ž„์‹œ์šฉ_ํšŒ์› ํƒˆํ‡ดํ•˜๊ธฐ<button>d</button></input>
  </form>

 

 

form ํƒœ๊ทธ์—๋Š” post๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด์ฃผ๊ณ , 

๊ทธ ์•ˆ์— inputํƒœ๊ทธ๋ฅผ ์ฃผ์–ด์„œ, ํ•ด๋‹น ํƒœ๊ทธ์˜ ์†์„ฑ์„ ์œ„์™€ ๊ฐ™์ด ์ ๊ณ  value์—๋Š” ์‹ค์ œ๋กœ ์š”์ฒญํ•˜๋ ค ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ ์–ด์ฃผ๋ฉด, ๋œ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์ด ๋ฐฉ๋ฒ•์˜ ๋‹จ์ ์€ router์—์„œ delete ๋ฉ”์†Œ๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ  post ๋ฉ”์†Œ๋“œ๋กœ ๋ฐ”๊ฟ”์ค˜์•ผ ํ•œ๋‹ค.

๊ทธ๋Ÿฌ๋‹ค ๋ณด๋‹ˆ ์ฃผ์†Œ๊ฐ’์ด ๊ฒน์น  ์šฐ๋ ค๊ฐ€ ์žˆ๋‹ค.

๊ทธ๋‹ฅ ์ข‹์€ ๋ฐฉ๋ฒ•์€ ์•„๋‹Œ๋“ฏ ํ–ˆ๋‹ค.

 

 

 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ์—ˆ๋‹ค.

npm i method-override 

 

์ด node.js ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•˜๋ฉด router์˜ ์š”์ฒญ์„ post ๋กœ ๋ฐ”๊พธ๊ณ , ์ฃผ์†Œ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์šด ์ผ์„ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

 

์•„๋ž˜๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ๊นƒํ—ˆ๋ธŒ ์‚ฌ์ดํŠธ์—์„œ ๊ฐ€์ ธ์˜จ ๋‚ด์šฉ์ด๋‹ค


์ฟผ๋ฆฌ ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ์žฌ์ •์˜

์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ๊ฐ’์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”์„œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ํ‚ค๋ฅผ ํ•จ์ˆ˜์— ๋Œ€ํ•œ ๋ฌธ์ž์—ด ์ธ์ˆ˜๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค methodOverride. ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ˜ธ์ถœํ•˜๋ ค๋ฉด POST์žฌ์ •์˜๋œ ๋ฉ”์„œ๋“œ๋ฅผ ํ•ด๋‹น ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ํ‚ค ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜์—ฌ URL์— ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค. <form>์ฟผ๋ฆฌ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ์ด ๋ฐฉ๋ฒ• ์€ ๋ ˆ๊ฑฐ์‹œ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง€์›ํ•˜๋ ค๊ณ  ํ•  ๋•Œ ์ผ๋ฐ˜์ ์œผ๋กœ ์ผ๋ฐ˜ HTML ์š”์†Œ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์ง€๋งŒ ์—ฌ์ „ํžˆ ์ตœ์‹  ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

var express = require('express')
var methodOverride = require('method-override')
var app = express()

// override with POST having ?_method=DELETE
app.use(methodOverride('_method'))

 

HTML์„ ์‚ฌ์šฉํ•œ ์ฟผ๋ฆฌ ์žฌ์ •์˜ ํ˜ธ์ถœ ์˜ˆ์‹œ <form>:

<form method="POST" action="/resource?_method=DELETE">
  <button type="submit">Delete resource</button>
</form>

 

action์— ์ฃผ์†Œ๊ฐ’์„ ์ ๊ณ , ๊ทธ ์ดํ›„๋กœ ?_method=์›ํ•˜๋Š”์š”์ฒญ  ์„ ์ ์–ด์ฃผ๋ฉด ํ•ด๊ฒฐ๋˜๊ธฐ์—

์˜คํžˆ๋ ค ๊ฐ„๋‹จํ•˜๋‹ค.

 

 

๐Ÿซ   ๋Š๋‚€์ 

ejs๋กœ ๋ฐฑ๊ณผ ํ”„๋ก ํŠธ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š”๊ฒŒ ์ฒ˜์Œ์ด๋ผ ์–ด๋ ค์› ์ง€๋งŒ,

์ด๋ฒˆ์— ๊ฒฝํ—˜ํ•ด ๋ดค์œผ๋‹ˆ ๋‹ค์Œ์—” ์ข€๋” ์ž˜ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ๊ฐ™๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ejs์˜  delete ์š”์ฒญ ๊ณผ ๊ฐ™์€ ๋ถ€๋ถ„์€ ์ข€ ๋ถˆํŽธํ•˜๋‹ค๊ณ  ๋Š๊ปด์กŒ์—ˆ๋Š”๋ฐ,

ํ•ด๋‹น ๋ชจ๋“ˆ์„ ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋‹ˆ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™๋‹ค.

 

๋‹ค๋งŒ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ”„๋ก ํŠธ์™€ ๋ฐฑ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฑด ์•„์ง ํ•ด๋ณด์ง€ ์•Š์•„์„œ 

๋‚˜์ค‘์— ํ•œ๋ฒˆ ๊ฒฝํ—˜ํ•ด ๋ณด๊ณ  ์‹ถ๋‹ค.

 

 

 

์ฐธ๊ณ ํ•œ ์‚ฌ์ดํŠธ

https://github.com/expressjs/method-override

 

GitHub - expressjs/method-override: Override HTTP verbs.

Override HTTP verbs. Contribute to expressjs/method-override development by creating an account on GitHub.

github.com

https://victorydntmd.tistory.com/29

 

[Node.js] sequelize(3) - CRUD + RESTful ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ

Sequelize ์‹œ๋ฆฌ์ฆˆ [Node.js] Sequelize(1) - sequelize์™€ sequelize-cli [Node.js] sequelize(2) - ๋ชจ๋ธ ์ •์˜ํ•˜๊ธฐ [Node.js] sequelize(3) - CRUD + RESTful ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ [Node.js] sequelize(4) - ๊ด€๊ณ„ ์„ค์ •( association ) ๋ฐ ๊ฒŒ์‹œํŒ ๋Œ“๊ธ€ ๊ตฌ

victorydntmd.tistory.com