O
O
OcelotJungle2021-04-10 09:41:17
Express.js
OcelotJungle, 2021-04-10 09:41:17

Why can a Node.js server crash?

I am using ExpressJS. I launch the HTTP server through the http module, I launch the HTTPS server through the spdy module, so that HTTP2, the application itself is based on Next.js, I use the Routes API, I launch it according to the "Custom Server" scheme, there is a request handler for dynamic images in the server.js file:

const dynamicFileHandler = async (req, res, next) => {
    try {
        const filePath = path.join(process.cwd() + decodeURIComponent(req.baseUrl));
        await fs.promises.access(filePath);
        res.sendFile(filePath);
    } catch(e) { next(); }
}
express.use("/images/*", dynamicFileHandler);

All other requests are handled by this snippet:
express.all("/api/*", nextHandler);
express.get("*", nextHandler);


And sometimes it happens that the application just crashes with the following message:
net.js:817
bytes += el.chunk instanceof Buffer ?
^

TypeError: Cannot read property 'chunk' of null
at Socket.bytesWritten (net.js:817:17)
at Socket.socketOnError (_http_server.js:637:31)
at Socket.emit (events.js:327:22)
at Stream.onStreamError (/root/app/node_modules/spdy/lib/spdy/handle.js:110:18)
at Stream.emit (events.js:315:20)
at errorOrDestroy (/root/app/node_modules/spdy-transport/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)
at onwriteError (/root/app/node_modules/spdy-transport/node_modules/readable-stream/lib/_stream_writable.js:424:5)
at onwrite (/root/app/node_modules/spdy-transport/node_modules/readable-stream/lib/_stream_writable.js:450:11)
at WritableState.onwrite (/root/app/node_modules/spdy-transport/node_modules/readable-stream/lib/_stream_writable.js:160:5)
at doWrite (/root/app/node_modules/spdy-transport/node_modules/readable-stream/lib/_stream_writable.js:409:30)


I have not yet been able to understand under what conditions and how to repeat this, it happens at random moments, but not spontaneously, but when requested (which, however, is logical, given the stack of errors) from the client.
Right now, I want to try to figure out what this line in net.js refers to, and find out if this is my mistake or some kind of nasty bug in Node itself. You can hang autorun, but this is a crutch, I want to decide humanly.
Can anyone help figure out where the problem might be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rPman, 2021-04-10
@rPman

open net.js file line 817 and add before this line (carefully choose the place) check el for null and call error handling
bytes += el.chunk instanceof Buffer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question