기타/에러

error: Cannot set headers after they are sent to the client

realtrynna 2022. 2. 27. 09:45

error: Cannot set headers after they are sent to the client

- 하나의 라우터에서 응답(res)을 2번이상 보낼 경우 발생(미들웨어 포함)

 

- 응답(send sendFile json 등) 2번 이상 보낼 경우 error
const express = require("express");
const path = require("path");
const app = express();

app.set("port", process.env.PORT || 3000);

app.get("/", (req, res) => {
  res.send("Node Server");
  res.sendFile(path.join(__dirname, "index.html"));
})

app.use((err, req, res, next) => {
  console.log("치명적인 에러 발생");
})

app.listen(app.get("port"), () => {
  console.log(3000);
})