4 글 보임 - 1 에서 4 까지 (총 4 중에서)
-
글쓴이글
-
2023년 1월 2일 14:31 #60545
Chu참가자안녕하세요 채팅창에서 본인이쓴거는 오른쪽에 뜨게끔하는 기능을 구현하려고하는데 제가생각한건 서버에서 유저아이디 받아온다음 본인이 쓴게 아니면 왼쪽에 본인이 쓴게 맞다면 오른쪽에 나오게끔하려했는데 방식이 옳지않은건지 계속 오류떠서 질문드립니다. chat.ejs eventSource = new EventSource('/message/' + 지금누른채팅방id); eventSource.addEventListener('test', function(e){ var 가져온거 = JSON.parse(e.data); 가져온거.forEach((i) => { if(data[i]._id == user[i].id) { $('.chat-content').append(`<li><span class="chat-box mine">${i.content}</span></li>`) } else if(data[i]._id != user[i].id) { $('.chat-content').append(`<li><span class="chat-box">${i.content}</span></li>`) } }); }) server.js
app.get('/message/:id', loginStatus, (req, res) => { res.writeHead(200, { "Connection": "keep-alive", "Content-Type": "text/event-stream", "Cache-Control": "no-cache", });
db.collection('message').find({ parent: req.params.id }).toArray().then((result) => { res.write('event: test\n'); res.write(`data: ${JSON.stringify(result)}\n\n`); res.send( {user : req.user}, {data : req.body} ) });
const pipeline = [ { $match : { 'fullDocument.parent': req.params.id } } ]; const collection = db.collection('message'); const changeStream = collection.watch(pipeline); changeStream.on('change', (result) => { res.write('event: test\n'); res.write(`data: ${JSON.stringify([result.fullDocument])}\n\n`); });
})
2023년 1월 3일 09:50 #60727
Chu참가자express deprecated res.send(status, body): Use res.status(status).send(body) instead server.js:331:13 node:internal/errors:484 ErrorCaptureStackTrace(err); ^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:393:5) at ServerResponse.setHeader (node:_http_outgoing:644:11) at ServerResponse.header (경로\codingapple\TodoApp\node_modules\express\lib\response.js:794:10) at ServerResponse.send (경로\codingapple\TodoApp\node_modules\express\lib\response.js:174:12) at ServerResponse.json (경로\codingapple\TodoApp\node_modules\express\lib\response.js:278:15) at ServerResponse.send (경로\codingapple\TodoApp\node_modules\express\lib\response.js:162:21) at 경로\codingapple\TodoApp\server.js:331:13 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { code: 'ERR_HTTP_HEADERS_SENT' 라고뜨고 챗룸에서 채팅방 클릭하면 채팅자체가 안떠버리네요
-
글쓴이글
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
- 답변은 로그인 후 가능합니다.