Skip to main content
100%

Using sendfile(2) with NodeJS

✓ Published0🌍 Public
Aalessioalex
Last edited Sep 17, 2010
Created on Mar 13, 2012

This example demonstrates using the `sendfile(2)` system call in NodeJS to stream file contents over a TCP connection, bypassing userspace buffering for efficiency. It creates a net server that opens a file with `process.binding('fs').open` and sends data in chunks via `sendfile`, tracking the file offset across invocations. The code handles `EAGAIN` errors by listening for the `'drain'` event and manually starting the socket’s `_writeWatcher` to resume transmission, ultimately ending the connection when the file is fully sent. The file path and port are supplied as command-line arguments.

AI-generated description

Similar vizzes