# Express ์‹œ์ž‘ํ•˜๊ธฐ

# Express Install

์„œ๋ฒ„๋ฅผ ์‰ฝ๊ฒŒ ์งค ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

npm install express
1
yarn add express
1

# Express setting

express ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์„œ๋ฒ„๋ฅผ ๋„์šฐ๊ธฐ ์œ„ํ•œ ๊ธฐ๋ณธ ์…‹ํŒ…

server.js;

const express = require('express'); // ์„ค์น˜ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
const app = express(); // express ์‹œ์ž‘ํ•˜๊ธฐ

app.listen(8080, function () {
  console.log('listening on 8080');
});
1
2
3
4
5
6
7
8

# listen ํ•จ์ˆ˜

listen ํ•จ์ˆ˜๋Š” ๋‘๊ฐ€์ง€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค. ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์„œ๋ฒ„๋ฅผ ๋„์šธ port ๋ฒˆํ˜ธ, ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์„œ๋ฒ„๋ฅผ ๋„์šด ํ›„ ์‹คํ–‰ํ•  ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

# server ๋„์šฐ๊ธฐ

> node server.js
  listening on 8080
1
2

์„œ๋ฒ„๋ฅผ ๋„์šธ ๋• ์„œ๋ฒ„ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ํŒŒ์ผ์„ node๋กœ ์‹คํ–‰์‹œ์ผœ์ฃผ๋ฉด ๋œ๋‹ค. ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌํ•œ ์ฝ˜์†”listening on 8080์ด ์ถœ๋ ฅ๋˜๋Š”๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

# server ์ ‘์†ํ•˜๊ธฐ

์œ„์—์„œ ์ž‘์„ฑํ•œ ํฌํŠธ ๋ฒˆํ˜ธ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ์ฐฝ์—์„œ localhost:port ์˜ port ์ž๋ฆฌ์— ์ž…๋ ฅํ›„ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์—ฐ๋‹ค.
ex / localhost:8080

# HTTP Methods

  • GET: getting data (์กฐํšŒ)
  • POST: creating data (์ƒ์„ฑ)
  • PUT: updating data (์ „์ฒด ์ˆ˜์ •)
  • DELETE: deleting data (์‚ญ์ œ)
  • PATCH: deleting data (์ˆ˜์ •)

# GET request

์„œ๋ฒ„์˜ ์›ํ•˜๋Š” ๊ฒฝ๋กœ๋กœ get์š”์ฒญ ์ฒ˜๋ฆฌ

/* with body-parser
 * ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์ˆ˜ ๋œป
 * req: request ์š”์ฒญ
 * res: response ์‘๋‹ต
 */
app.get('/path', function (request, response) {
  response.send('hello');
});
1
2
3
4
5
6
7
8

์œ„์˜ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•ด locathost:8080/path๋กœ ์ ‘์†ํ•˜๋ฉด hello ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

# ์„œ๋ฒ„์—์„œ HTML ํŒŒ์ผ ์ „๋‹ฌํ•˜๊ธฐ

app.get('/path', function (request, response) {
  response.sendFile(__dirname + '/index.html');
});
1
2
3

# POST request

POST ์š”์ฒญ์œผ๋กœ ํ•ด๋‹น ๊ฒฝ๋กœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ „์†ก

POST ์š”์ฒญ์œผ๋กœ ํ•ด๋‹น ๊ฒฝ๋กœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ „์†กํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. req.body์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์„œ ๋ณด๋‚ธ๋‹ค.

app.post('/path', function (req, res) {
  var post = req.body;
});
1
2
3

# PUT request

๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” Method ๋ฐ์ดํ„ฐ์™€ ์กฐ๊ฑด์„ ๋ณด๋‚ด๋ฉด, ์กฐ๊ฑด์— ๋งž๋Š” ์ •๋ณด๋ฅผ ์ฃผ์–ด์ง„ ๋ฐ์ดํ„ฐ๋กœ ์ˆ˜์ •ํ•œ๋‹ค.

app.put('/path', function (req, res) {
  ...
}
1
2
3

# DELETE request

๋‹จ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉ ๋ณดํ†ต Query Params๋‚˜ Path Variables์™€ Body๋ฅผ ํ˜ผํ•ฉํ•ด์„œ ์ž˜ ์‚ฌ์šฉํ•œ๋‹ค. ์กฐ๊ฑด์— ๋งž๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ

app.delete('/path', function (req, res) {
  ...
}
1
2
3

# + express ์—์„œ status code ๋„ฃ๊ธฐ

express์—์„œ ์ง์ ‘ ์‘๋‹ต์ฝ”๋“œ๋ฅผ ์š”์ฒญํ•  ์ˆ˜ ์žˆ๋‹ค.

# status()

ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์‘๋‹ต์ฝ”๋“œ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ๋™์‹œ์— ์ด์™€ ๊ฐ™์ด sendํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด cmd ์— ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

app.get('/', (req, res) => {
  res.status(200).send('hello world');
});
1
2
3

๋˜๋Š” ์ด์™€ ๊ฐ™์ด send๋ฅผ ์‚ฌ์šฉํ•ด message ์ง์ ‘ ์ „๋‹ฌํ•ด ๊ฐœ๋ฐœ์ž๋„๊ตฌ์˜ console์— ๋ฉ”์„ธ์ง€๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.

app.get('/', (req, res) => {
  res.status(200).send({ message: 'success' });
});
1
2
3

# ๊ฒฝ๋กœ ์ด๋™ํ•˜๊ธฐ

# res.redirect()

์š”์ฒญ์˜ ๊ฒฝ๋กœ๋ฅผ ์žฌ์„ค์ •ํ•œ๋‹ค. ์ด ์˜ˆ์ œ๋Š” /edit ๊ฒฝ๋กœ์—์„œ <form> ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ • ํ›„ **redirect**๋ฅผ ์‚ฌ์šฉํ•ด ๊ฒฝ๋กœ๋ฅผ ์žฌ์„ค์ • ํ•œ ์˜ˆ์ œ์ด๋‹ค.

app.put('/edit', (req, res) => {
  db.collection('post').updateOne(
    { _id: parseInt(req.body.id) },
    { $set: { title: req.body.title, date: req.body.date } },
    (err, result) => {
      console.log('done fixing it');
      res.redirect('/list'); // reset the path
    }
  );
});
1
2
3
4
5
6
7
8
9
10

# app.use()

express์—์„œ ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ app.use(~)๋ฅผ ์ž‘์„ฑํ•˜๋ฉด "๋‚˜๋Š” ๋ฏธ๋“ค์›จ์–ด๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค" ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

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

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));

const methodOverride = require('method-override');
app.use(methodOverride('_method'));
1
2
3
4
5
6
7
8

์œ„์ฒ˜๋Ÿผ ๋‹ค์–‘ํ•œ ๋ฏธ๋“ค์›จ์–ด๋“ค์„ app.use๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

# Express middleware

Express ๋ฏธ๋“ค์›จ์–ด๋ž€ ์‰ฝ๊ฒŒ๋งํ•ด ํ•จ์ˆ˜์ด๋‹ค.Express์—์„œ๋Š” ์‚ฌ์‹ค ๋ชจ๋“ ๊ฒƒ๋“ค์ด ๋ฏธ๋“ค์›จ์–ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๋Š” ์š”์ฒญ(req), ์‘๋‹ต(res), ๊ทธ๋ฆฌ๊ณ  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์š”์ฒญ-์‘๋‹ต ์ฃผ๊ธฐ ์ค‘ ๊ทธ ๋‹ค์Œ์˜ ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜์— ๋Œ€ํ•œ ์•ก์„ธ์Šค ๊ถŒํ•œ์„ ๊ฐ–๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๊ทธ ๋‹ค์Œ์˜ ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ next๋ผ๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋กœ ํ‘œ์‹œ๋œ๋‹ค.

Last Updated: 2022. 6. 5. ์˜คํ›„ 3:42:39