How to delay response until POST payload is validated

BanksySan

How do I delay returning the response until I have read the payload and confirmed it is a good request?

In the following code, the method is returning before the data event is fired, thus it is always 200.

http.createServer(function(request, response) {
    var payloadValid = true;        // <-- Initialise an 'payloadValid' variable to true

    request.on('data', function(chunk) {
        payloadValid = false;       // <-- set it to true when the payload is examined
    });

/*
 * This is executed before the payload is validated
 */

    response.writeHead(payloadValid ? 200 : 400, {    // <-- return 200 or 400, depending on the payloadValid  variable
        'Content-Length': 4,
        'Content-Type': 'text/plain'
    });
    response.write('Yup!');
    response.end();
})
.listen(PORT_NUMBER);
enolam

I would just put the response method into the function callback. Code below. Works in postman.

var http = require('http');

http.createServer(function(request, response) {
    var payloadValid = true;     

    request.on('data', function(chunk) {
        payloadValid = false;
        response.writeHead(payloadValid ? 200 : 400, {   
            'Content-Type': 'text/plain'
        });
        response.write('Yup!');
        response.end();     
    });
})
.listen(8080);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Post Values to an alert box after been validated

분류에서Dev

Constructing payload in AngularJs POST calls

분류에서Dev

Constructing payload in AngularJs POST calls

분류에서Dev

Using Ember Data RestSerializer to POST to a 3rd-party API, how can I change the payload format?

분류에서Dev

Delay pie chart animation until in viewport

분류에서Dev

Ember: jQuery Post with nested JSON-Payload

분류에서Dev

How can I return a response to an AngularJS $http POST to Sinatra?

분류에서Dev

Bash script does not execute commands until after delay

분류에서Dev

python requests: Retrying until a valid response is received

분류에서Dev

Add a delay in git post-merge

분류에서Dev

TIdHTTP character encoding of POST response

분류에서Dev

How to use POST method in rest-assured and fetch the value from its response

분류에서Dev

Get HTTP POST Response in Rails Controler

분류에서Dev

Pubsubhubub to get "POST" response from the hub

분류에서Dev

Ember : 중첩 된 JSON-Payload가있는 jQuery Post

분류에서Dev

How to handle with Spring BindingResult exception from inner @Entity class validated with Hibernate before persistence

분류에서Dev

How to send original message payload to jms error queue

분류에서Dev

How to get local path for payload in WiX/Burn Managed Bootstrapper Application?

분류에서Dev

How do I add a delay to the mouseover function?

분류에서Dev

how to run script on remote machine with delay

분류에서Dev

How to create json response

분류에서Dev

How to send multipart response?

분류에서Dev

How to retry until success or timeout asynchronously?

분류에서Dev

How to run a loop until an exception is not generated

분류에서Dev

how to disable the radio group until checkbox is selected

분류에서Dev

How to not update a model until form submission in Angular?

분류에서Dev

RestKit doesn't like mapping a different object for a POST response

분류에서Dev

How to alter a API response with Javascript

분류에서Dev

How to put SOAP response into UITableView?

Related 관련 기사

  1. 1

    Post Values to an alert box after been validated

  2. 2

    Constructing payload in AngularJs POST calls

  3. 3

    Constructing payload in AngularJs POST calls

  4. 4

    Using Ember Data RestSerializer to POST to a 3rd-party API, how can I change the payload format?

  5. 5

    Delay pie chart animation until in viewport

  6. 6

    Ember: jQuery Post with nested JSON-Payload

  7. 7

    How can I return a response to an AngularJS $http POST to Sinatra?

  8. 8

    Bash script does not execute commands until after delay

  9. 9

    python requests: Retrying until a valid response is received

  10. 10

    Add a delay in git post-merge

  11. 11

    TIdHTTP character encoding of POST response

  12. 12

    How to use POST method in rest-assured and fetch the value from its response

  13. 13

    Get HTTP POST Response in Rails Controler

  14. 14

    Pubsubhubub to get "POST" response from the hub

  15. 15

    Ember : 중첩 된 JSON-Payload가있는 jQuery Post

  16. 16

    How to handle with Spring BindingResult exception from inner @Entity class validated with Hibernate before persistence

  17. 17

    How to send original message payload to jms error queue

  18. 18

    How to get local path for payload in WiX/Burn Managed Bootstrapper Application?

  19. 19

    How do I add a delay to the mouseover function?

  20. 20

    how to run script on remote machine with delay

  21. 21

    How to create json response

  22. 22

    How to send multipart response?

  23. 23

    How to retry until success or timeout asynchronously?

  24. 24

    How to run a loop until an exception is not generated

  25. 25

    how to disable the radio group until checkbox is selected

  26. 26

    How to not update a model until form submission in Angular?

  27. 27

    RestKit doesn't like mapping a different object for a POST response

  28. 28

    How to alter a API response with Javascript

  29. 29

    How to put SOAP response into UITableView?

뜨겁다태그

보관