๐Ÿค” Node.js Response Error

lim1313ยท2021๋…„ 10์›” 14์ผ
0

ERROR ๋ฆฌ๋ทฐ

๋ชฉ๋ก ๋ณด๊ธฐ
7/8

๐Ÿ˜ต Error

์ƒํ™ฉ

POST /users/:userId/orders ์— ์š”์ฒญ์„ ๋ณด๋ƒˆ์„ ๋•Œ, ์ •์ƒ์ ์ธ ์š”์ฒญ์ด๋ผ๋ฉด 500or 201์œผ๋กœ ์‘๋‹ต๋˜์–ด์•ผ ํ•˜๊ณ ,
์ •์ƒ์ ์ธ ์š”์ฒญ์ด ์•„๋‹ˆ๋ผ๋ฉด 400์œผ๋กœ ์‘๋‹ต๋˜์–ด์•ผ ํ•œ๋‹ค.

๊ตฌํ˜„ ์ฝ”๋“œ

    post: (req, res) => {
      const userId = req.params.userId;
      const { orders, totalPrice } = req.body;

      if (!!orders && !!totalPrice) {
        models.orders.post(userId, orders, totalPrice, (error, result) => {
          if (error) {
            res.status(500).send('Internal Server Error');
          } else {
            res.sendStatus(201);
          }
        });
      }
         res.status(400).json('badbad');
    },

์‹คํ–‰

๋ฌธ์ œ

๋ฌธ์ œ ๋ฐœ์ƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์ •์ƒ์ ์ธ ์š”์ฒญ์œผ๋กœ ๋ณด๋‚ด์—ˆ์„ ๋–„ response๊ฐ€ 400์ด ๋ฐœ์ƒํ•œ๋‹ค.

๐Ÿค” ๋ฌธ์ œ ๋ถ„์„

์—๋Ÿฌ ๋ฉ”์‹œ์ง€

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

๋‚˜์˜ ์ฐฉ๊ฐ

ํ•œ๋ฒˆ์˜ response๊ฐ€ ์‹คํ–‰๋˜๋ฉด ๋œ๋‹ค๋ฉด ๋’ค์˜ ์ฝ”๋“œ๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค. ์ฆ‰, return ํ•ด์ค€ ๊ฒƒ๊ณผ ๊ฐ™์ด, ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋ฒ„๋ฆฐ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์˜€๋‹ค.

์‚ฌ์‹คํŒŒ์•…

์œ„์™€ ๊ฐ™์ด ์“ด๋‹ค๋ฉด res.sendStatus(201)์™€ res.sendStatus(400)๋ชจ๋‘ ์‹คํ–‰๋œ๋‹ค.
์ฆ‰, ๋‘๋ฒˆ์˜ response๊ฐ€ ๋ณด๋‚ด์ง€๊ณ , ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

๊ฐ„๋‹จํ•œ ํ™•์ธ์„ ์œ„ํ•ด ๋‹ค์Œ์„ ์‹คํ–‰ํ•ด ๋ณธ๋‹ค๋ฉด
import express from 'express';

const app = express();
app.use(express.json());

app.get('/users/:id', (req, res, next) => {
  let id = req.params.id;
  if (id === 'a') {
    console.log(`before sending 200 status`);
    res.status(200).send('success');
  }
  console.log('after sending 200 status , id', id);
  res.status(404).send('failed');
});

app.listen(8080);

GET /users/1 ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›๊ฒŒ ๋œ๋‹ค.
before sending 200 status
after sending 200 status , id a

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

if๋ฌธ ์•ˆ์˜ console๊ณผ if๋ฌธ ๋ฐ–์˜ console ๋ชจ๋‘๊ฐ€ ์‹คํ–‰๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์—๋Ÿฌ๋ฉ”์‹œ์ง€ : ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ณด๋‚ธ ํ›„ ๋‹ค์‹œ headers๋ฅผ setํ•  ์ˆ˜ ์—†๋‹ค.

์ฆ‰, response๊ฐ€ ์ด๋ฃจ์–ด์กŒ๋‹ค๊ณ  ํ•ด์„œ ๋ฏธ๋“ค์›จ์–ด ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋‹ค. ์ดํ›„์— ์ฝ”๋“œ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ์ฝ”๋“œ๊ฐ€ ์ด์–ด์„œ ์‹คํ–‰๋œ๋‹ค.


๐Ÿ˜Ž ๊ฐœ์„ 

 post: (req, res) => {
      const userId = req.params.userId;
      const { orders, totalPrice } = req.body;

      if (!!orders && !!totalPrice) {
        models.orders.post(userId, orders, totalPrice, (error, result) => {
          if (error) {
            res.status(500).send('Internal Server Error');
          } else {
            res.sendStatus(201);
          }
        });
      } else {
        res.sendStatus(400);
      }
    }

if else๋ฌธ์œผ๋กœ ๋ถ„๊ธฐ๋ฅผ ํ•ด์ฃผ๊ฑฐ๋‚˜ return๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ๋ฒˆ์˜ response๋งŒ ๋ณด๋‚ด์ง€๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

์‹คํ–‰


์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต์„ ๋ฐ›์•„์˜จ๋‹ค.

profile
start coding

0๊ฐœ์˜ ๋Œ“๊ธ€