-1

I have a webapp created with Node.js/Express.js/Pug that runs a bash script(mostly an Nmap scan) and displays the results. I'd like to implement some sort of page in between the start and the results to signify the system is working on the task.

I tried to just add another res.render(...) at the beginning of the route that starts the scan, but I ran into the problem that HTTP cannot send headers twice. Effectively, I can't send two http responses for one request; please let me know if I'm wrong here.

I'm still not very familiar with this stuff; I'm working with a group and this job fell to me, any help is appreciated.

Liam
  • 27,717
  • 28
  • 128
  • 190
  • *I can't send two http responses for one request*, no you can't because that's impossible. [HTTP is a single request/response protocol](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview#http_flow). I'd suggest you read up on [how HTTP works](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview) – Liam Feb 18 '21 at 15:15

1 Answers1

3

Typically the route handler would:

  • trigger the long running script asynchronously
  • return an "in progress" page

Then the "in progress" page would ask the server if it was done yet via:

  • Websocket
  • Ajax polling
  • Meta refresh polling

You'd need to have the callback to the original asynchronous process keep track of where the response should go to (possibly using a GUID that would be passed to it and also returned as data in the "in progress" page).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335