Welcome to the Linux Foundation Forum!

In what situations do we need to invoke end() after request being sent?

Hi, I am learning 05. CREATING RESTFUL JSON SERVICES and follow the examples carefully.

For the fastify part of the tutorial, when I need to check what would the response be if I POST request to a valid GET route.

I noticed that there is a trailling .end() in the code:

node -e "http.request('http://localhost:3000/bicycle/1', { method: 'post'}, ({statusCode}) \
=> console.log(statusCode)).end()"

And it turns out that .end() is necessary. The command will hang without it. So I am curious about .end(). Why I don't need it in other situations? What exactly do to avoid the request from hanging?

Thank you!

Best Answer

  • xdxmxc
    xdxmxc Posts: 61
    Answer ✓

    correct, end in this scenario means "I'm done writing to the client" - which allows the request to complete


  • baocongchen
    baocongchen Posts: 15
    edited February 13

    I think it's similar to the response/reply mechanism. You either put .end() or .send('') if you want to send the response status without any content.


Upcoming Training