在Node.js中验证错误后重定向POST数据

时间:2011-12-01 15:00:28

标签: node.js routing coffeescript express

有没有办法将数据从POST函数中的请求转移到GET函数的资源?

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

我需要验证一些表单数据,并希望预设在POST之前输入的数据。

1 个答案:

答案 0 :(得分:3)

您不应该这样做,而是应该使用中间件功能,例如:

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