1

Is there any way to shift data from a request in a POST function to a resource of a GET function?

app.get '/form', routes.getForm
app.post '/form', routes.postForm
app.get '/form', routes.getForm  # pass return data from previous POST call

I need to validate some form data and would like to preset data that has been entered before the POST.

1 Answer 1

4

You shouldn't do it that way, instead you should be using a middleware function, for example:

validate = (req, res, next) ->
  # validate your req.body stuff here
  ...
  if data_is_valid
    next() # proceed to the next function
  else
    res.redirect '/form?data=invalid'

app.post '/form', validate, routes.getForm
Sign up to request clarification or add additional context in comments.

2 Comments

But how can you persist the form data that user entered previous the error valdiation and show it to the user when rendering the error? When res.redirect is executed attributes.body get resets.
You can store it on the session and then delete it each time after the form page is rendered. This is how github.com/jaredhanson/connect-flash works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.