2015-01-26 65 views
0

我有一個web應用程序,它發送我的快遞服務器的MongoDB ObjectIDs POST請求。然後我使用這些ObjectID來使用Mongoose從我的數據庫中選擇文檔。如何處理Express和Node中的服務器端驗證?

我目前正在通過從Mongoose的回調中獲取錯誤來處理錯誤,從路由處理函數返回,然後調用通用錯誤函數來響應狀態和錯誤。

if (app.get('env') === 'development') { 
    app.use(function(err, req, res, next) { 
     console.log(err); 
     res.status(err.status || 500); 
     res.send({ 
      message: err.message, 
      error: err 
     }); 
    }); 
} 

我很好奇,無論是使用類似Validate.js來檢查對象ID是有效的餵養它的貓鼬或簡單地允許貓鼬看到它是不是有效的,並允許它回調之前很好的做法一個錯誤,然後我與我的通用錯誤處理程序處理?

我很想好奇地聽到任何解釋。

回答

1

http://mongoosejs.com/docs/validation.html

貓鼬有一些內置的驗證器。

All SchemaTypes have the built in required validator. 
Numbers have min and max validators. 
Strings have enum and match validators. 

例如,

var mySchema = new mongoose.Schema({ 
    myField: {type String, match:'/\.html$/'} 
    myNumField: {type Num, max:5} 
}); 

我想你應該用自己的方法,因爲這是它做什麼。