request.writableEnded Property in Node.js
0 198
Node.js provides developers with powerful features to manage network communication. One such capability includes monitoring and controlling data streams. In this article, we'll dive into the request.writableEnded
property, a useful tool when working with HTTP request streams in Node.js.
What is request.writableEnded
?
The request.writableEnded
property is a Boolean flag that indicates whether the writable side of the HTTP request stream has finished. If it returns true
, it means that the end()
method has been called and no more data will be written to the request stream.
This property helps track the state of the request stream, making it easier to avoid errors when writing or ending streams multiple times.
Why Use request.writableEnded
?
When managing HTTP requests, especially in cases where data is written manually to the request stream, it’s important to know whether the stream has already ended. Trying to write data after the stream has ended will throw an error. By checking request.writableEnded
, you can make your code safer and more predictable.
Syntax
The syntax to use this property is straightforward:
request.writableEnded
This returns a Boolean value—true
if the stream has been ended, false
otherwise.
Example: Using writableEnded
in an HTTP Server
Here's a basic example of how this property can be used in an HTTP server scenario:
const http = require('http');
const server = http.createServer((req, res) => {
res.write('Hello, client!');
if (!res.writableEnded) {
res.end(' Connection closed.');
}
console.log('Writable ended:', res.writableEnded);
});
server.listen(3000, () => {
console.log('Server is running on port 3000');
});
In this example, we check whether the response stream has already been ended using writableEnded
before calling end()
. This avoids potential runtime errors.
Output
When you run this code and open http://localhost:3000
in your browser, you’ll see the following in your terminal:
Server is running on port 3000
Writable ended: true
And the client will receive:
Hello, client! Connection closed.
Real-World Use Cases
- Prevent writing or ending a response multiple times in a complex logic flow.
- Use in middleware or custom request handlers to ensure proper stream handling.
- Validate stream state before applying transformations or piping data.
Conclusion
The request.writableEnded
property is a handy utility when working with HTTP streams in Node.js. It gives you better control over the request lifecycle and helps prevent issues like writing to an already closed stream. Whether you’re building APIs, proxies, or real-time systems, knowing the status of your streams is key—and this property makes it easier.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!

Share:
Comments
Waiting for your comments