강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của jjarlit
jjarlit

câu hỏi đã được viết

Một trại huấn luyện phát triển toàn diện thực sự dành cho những người không chuyên ngành

Xây dựng máy chủ Grab Market

nodejs sever 질문

Viết

·

1.2K

1

'그랩마켓서버구축하기' 강의에서 localhost:8080에 접속 후 /products로 진입하면 처음에는 렌더링 되지만 이후 터미널에서  events.js:292

      throw er; // Unhandled 'error' event

      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end

    at writeAfterEnd (_http_outgoing.js:668:15)

    at ServerResponse.end (_http_outgoing.js:788:7)

    at Server.<anonymous> (C:\Users\JAEHA\Desktop\grab-market-server\index.js:24:7)

    at Server.emit (events.js:315:20)

    at parserOnIncoming (_http_server.js:874:12)

    at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)

Emitted 'error' event on ServerResponse instance at:

    at writeAfterEndNT (_http_outgoing.js:727:7)

    at processTicksAndRejections (internal/process/task_queues.js:81:21) {

  code: 'ERR_STREAM_WRITE_AFTER_END'

}

이런 에러가 뜨면서 서버가 다운됩니다. 구글링해서 해봤는데도 효과가 없습니다.. vscode도 종료했다 켰어요!

tensorflowreact-nativejavascriptreact머신러닝 배워볼래요? nodejsexpressHTML/CSS

Câu trả lời 6

4

grab님의 프로필 이미지
grab
Người chia sẻ kiến thức

다행이네요! Window 환경에서 가끔씩 동작이 다를 때가 있어 저도 수업자료에 넣어두겠습니다.

감사합니다 😁

3

grab님의 프로필 이미지
grab
Người chia sẻ kiến thức

var http = require("http");
var hostname = "127.0.0.1";
var port = 8080;

const server = http.createServer(function (req, res) {
  const path = req.url;
  const method = req.method;
  if (path === "/products") {
    if (method === "GET") {
      res.writeHead(200, { "Content-Type": "application/json" });
      const products = JSON.stringify([
        {
          name: "농구공",
          price: 5000,
        },
      ]);
      res.end(products);
    } else if (method === "POST") {
      res.end("생성되었습니다!");
    }
  }else{
    res.end("Good Bye");
} }); server.listen(port, hostname); console.log("grab market server on!");

위와 같이 코드 변경해보시겠어요?
바꾼 부분은 else를 추가했습니다.

if(path == '/products'){

  ...

} else{

  res.end("Good Bye");

}

1

JJARLIT님의 프로필 이미지
JJARLIT
Người đặt câu hỏi

해결됐습니다!! 더 찾아보고 고민했어야됐는데... 이렇게 간단하다니 ... 감사합니다!

0

JJARLIT님의 프로필 이미지
JJARLIT
Người đặt câu hỏi

index.js 입니다 참고로 깃허브에 올리신거 복사해서 해봤는데도 똑같은 오류가 났습니다 ㅠ

0

JJARLIT님의 프로필 이미지
JJARLIT
Người đặt câu hỏi

var http = require("http");
var hostname = "127.0.0.1";
var port = 8080;

const server = http.createServer(function (req, res) {
  const path = req.url;
  const method = req.method;
  if (path === "/products") {
    if (method === "GET") {
      res.writeHead(200, { "Content-Type": "application/json" });
      const products = JSON.stringify([
        {
          name: "농구공",
          price: 5000,
        },
      ]);
      res.end(products);
    } else if (method === "POST") {
      res.end("생성되었습니다!");
    }
  }
  res.end("Good Bye");
});

server.listen(port, hostname);

console.log("grab market server on!");

0

grab님의 프로필 이미지
grab
Người chia sẻ kiến thức

/products 라우터 전체 코드 올려주시면 빠르게 확인이 가능할 것 같아요~!
아니면 해당 소스코드 파일을 첨부로 올려주셔도 좋습니다.

Hình ảnh hồ sơ của jjarlit
jjarlit

câu hỏi đã được viết

Đặt câu hỏi