2011-12-01 32 views
0

有什麼方法將數據從POST函數中的請求轉移到GET函數的資源?在Node.js中驗證錯誤後重定向POST數據

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

我需要驗證一些表單數據並且想要預設在POST之前輸入的數據。

回答

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 
+0

但你怎麼能堅持表單數據用戶輸入以前的錯誤valdiation並顯示給用戶何時渲染錯誤?當res.redirect被執行時,attributes.body得到重置。 – p1nox

+1

您可以將其存儲在會話中,然後在表單頁面呈現後每次刪除它。這是https://github.com/jaredhanson/connect-flash如何工作。 – alessioalex

+0

是的,我已經實施了這種方法,謝謝! – p1nox