-
toyproject 에러 해결1 :Error [ERR_STREAM_WRITE_AFTER_END]: write after end카테고리 없음 2021. 8. 31. 12:48
var http=require('http');
var fs = require('fs');
var url= require('url');
var template=require('./lib/template.js');
var app= http.createServer(function(request,response){
var _url =request.url;
var querydata = url.parse(_url,true).query;
var pathname = url.parse(_url, true).pathname;
var path =require('path');
/*var temp=`
<!doctype html>
<html>
<head>
<title>Wyh</title>
</head>
<body>
<h1>css</h1>
</body>
</html>
`;*/
//response.end(temp);
console.log(pathname);
if(pathname==='/'){
console.log(querydata);
if(querydata.id=== undefined){
console.log('hello');
fs.readdir('./shop',function(error,filelist){
//if(error){throw error;}
console.log(filelist);
var title='abc';
var description = 'Thing!';
var list = template.list(filelist);
var html= template.HTML(title,list,`<h2>${title}</h2>${description}`
,`<a href ="/create">create</a>`);
response.writeHead(200);
response.end(html);
})
}
}/*else if(pathname.id==='/create'){
console.log('welcome');
}
else{
response.end('Not Found');
response.writeHead(404);
}
if(request.url == '/favicon.ico'){
response.writeHead(404);
response.end();
}
response.writeHead(200);
response.end(querydata.id);*/
});
app.listen(3007);
이 부분에서
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_http_outgoing.js:694:15)
at ServerResponse.end (_http_outgoing.js:815:7)
at C:\Users\A\Desktop\toy project1\m.js:37:22
at FSReqCallback.oncomplete (fs.js:180:23)
Emitted 'error' event on ServerResponse instance at:
at writeAfterEndNT (_http_outgoing.js:753:7)
at processTicksAndRejections (internal/process/task_queues.js:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}에러가 자주 떠서, port가 이미 실행되고 있는지 확인해봤는데, 아니였다. 그렀다면, write after end 때문이였는데,
response.writeHead(200);
response.end(querydata.id);*/ 마지막 이 부분을 주석처리 해줬더니 해결되었다.
Node.js "write after end" error | Newbedev 이것과 연관이 있나 싶다.
The first time response.write() is called, it will send the buffered header information and the first body to the client. The second time response.write() is called, Node.js assumes you're going to be streaming data, and sends that separately. That is, the response is buffered up to the first chunk of body.
So, if you want to send only one chunk, you must use response.end(body), and if you want to send multiple chunks you must end you called with the response.end() after multiple response.write().
라 나와 있는데, 아직 실력이 부족해서 좀 더 공부를 해봐야겠다.