sinammd
sinammd

Reputation: 132

chain request and respond nodejs(express framework)

i want to receve a get request from user and respond a picture with number in it to that user. and then receve number in the picture from user and if the number true send back some data to that user. and i dont want to use CAPCHA services. how can i handle request and respond with expressjs?

Upvotes: 0

Views: 49

Answers (1)

jfriend00
jfriend00

Reputation: 707786

Here would be the basic steps:

  1. Install express-session so you can have server-state per client
  2. Client sends GET request to get image
  3. Generate random number
  4. Store random number and current server time/date in client session object
  5. Create or select the appropriate image that contains the previously selected random number
  6. Send the image as the response to the initial GET request
  7. Client sends a POST request with the number that the user typed probably as a form POST.
  8. Server gets the POST request data out of the body and compares the submitted number to the number in the req.session object and also checks to see if the current server time is close enough to the time/date in the session so the previous data has not yet expired. If both tests pass, then client has appropriately submitted the number in the picture and you can set a flag in req.session that indicates this client has passed. If both tests don't pass, then you clear the number and date/time in req.session and send a response that indicates failure.
  9. Future requests from this client can check the session object to see if this client has recently passed the image number validation or not. You may or may not want to set an expiration on the image validation.

Upvotes: 1

Related Questions