L
L
lukka2020-05-26 19:06:35
Node.js
lukka, 2020-05-26 19:06:35

ERR_STREAM_WRITE_AFTER_END how to solve?

During development, everything worked, when I launched Ubuntu on the VPS, an error occurred in the console

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'                                             
}


Code snippet referenced by the error
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))

                    }
                }
            }
        }
    })
})

In short - in this fragment, depending on the url requested from the client, different files are given.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question