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:

  1. node -e "http.request('http://localhost:3000/bicycle/1', { method: 'post'}, ({statusCode}) \
  2. => 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

  • Posts: 160
    Answer ✓

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

Answers

  • Posts: 15
    edited February 2022

    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.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training