STREAM WRITE AFTER END при запуске на VPS
При разработке все работало, когда запустил на VPS на Ubuntu 16 возникла ошибка в консоли.
events.js:292
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at writeAfterEnd (_http_outgoing.js:643:15)
at ServerResponse.end (_http_outgoing.js:763:7)
at /home/puryga/node/server.js:1334:29
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_co
ntext.js:63:3)
Emitted 'error' event on ServerResponse instance at:
at writeAfterEndNT (_http_outgoing.js:702:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
Фрагмент кода на который ссылается ошибка
let filePath = path.join(process.cwd(), '/public', pathName)
fs.readFile(filePath, 'utf-8', (err, html) => {
if(err) {
nopath = path.join(process.cwd(), '/', 'nopath.html')
fs.readFile(nopath, (err , html) => {
if(!err) {
res.writeHead(404, {'Content-Type': 'text/html'})
res.end(html)
}
else{
let text = "404 file open error"
res.writeHead(404, {'Content-Type': 'text/plain'})
res.end(text)
}
})
}
else{
for (let it in mimeTypes) {
if (mimeTypes.hasOwnProperty(it)) {
if (ext == it) {
res.writeHead(200, {'Content-Type': mimeTypes[ext]})
filePath = path.join(process.cwd(), '/public', pathName)
fs.readFile(filePath, (err , content) => {
if(!err) {
res.writeHead(200, {'Content-Type': mimeTypes[ext]})
res.end(content)
}
else{
res.writeHead(404, {'Content-Type': 'text/plain'})
res.end()
}
})
} else if (ext == '.html') {
const content = require(filePath)
let p = ''
if (req.url.split('?')[1] != '') {
p = req.url.split('?')[1]
}
res.writeHead(200, {'Content-Type': 'text/html'})
res.end(content.getContent(SESSION_KEY, p))
}
}
}
}
})
})
В кратце - в этом фрагменте, в зависимости от запрашиваемого с клиента url отдаются разные файлы.