2014-02-25 59 views
0

我想重定向到一個404頁面,如果沒有找到一個id我想重定向到一個404頁面,如果沒有找到一個id

頁路線/ users.js

exports.find = function(req, res) { 
     var id =(req.params.id); 
    db.getFindById(id,function(err, results){ 
      if (err) return res.send(500, "DB QUERY ERROR"); 
      res.render('usr/details', { finds: results }); 

                }); 
             } 

頁index.js

app.configure(function(){ 
    app.set('views', __dirname +'/views'); 
    app.set('view engine','jade'); 
    app.use(express.bodyParser({KeepExtensions: true, uploadDir: path.join(__dirname, '/public/img/user-avatar')})); 
    app.use(express.methodOverride()); 
    app.use(checkDatabase, function(req, res) {res.send(404, "Could not find ")}); 
    app.use(passport.initialize()); 
    app.use(express.static(path.join(__dirname, 'public'))); 
         }); 


app.get('/list-users/finds/:id',checkDatabase); 

回答

0

你必須在參數列表中,表達將通過增加nextŧ你的處理程序。它將轉移到中間件堆棧中的下一個功能。

然後,如果沒有id比賽,做return next()離開此功能,並移動到將要處理功能的404

exports.find = function (req, res, next) { 
    var id = (req.params.id); 
    db.getFindById(id, function (err, results) { 
     // Query failed, note the return to quit the function here. 
     if (err) return res.send(500, "DB QUERY ERROR"); 

     // Move to next function in middleware steck if result set is empty 
     if (!results || !results.length) return next(); 

     // We will not get to this call unless the resultset is populated 
     res.render('usr/details', { 
      finds: results 
     }); 
    }); 
} 

然後,您可以use這個後一個新的功能,這將運行在調用next的情況下。

app.use(user.find, function(req, res) { 
    res.send(404, "Could not find ") 
}; 

編輯:你propably想:

index.js

app.configure(function() { 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.bodyParser({ 
     KeepExtensions: true, 
     uploadDir: path.join(__dirname, '/public/img/user-avatar') 
    })); 
    app.use(express.methodOverride()); 
    app.use(passport.initialize()); 
    app.use(express.static(path.join(__dirname, 'public'))); 
}); 

app.get('/list-users/finds/:id', user.find, function (req, res) { 
    res.send(404, "Could not find ID"); 
}); 

路線/ users.js

exports.find = function (req, res, next) { 
    var id = (req.params.id); 
    db.getFindById(id, function (err, results) { 
     // Query failed, note the return to quit the function here. 
     if (err) return res.send(500, "DB QUERY ERROR"); 

     // Move to next function in middleware steck if result set is empty 
     if (!results || !results.length) return next(); 

     // We will not get to this call unless the resultset is populated 
     res.render('usr/details', { 
      finds: results 
     }); 
    }); 
} 
+0

的ReferenceError:未定義checkDatabase – rokirakan78

+0

的當然你必須用上面的函數名來替換它。我不知道你需要導出模塊的變量名稱是什麼。 – MildlySerious

+0

app.configure(函數(){ \t app.set( '視圖',__dirname + '/視圖'); \t app.set( '視圖引擎', '玉'); \t app.use(表達.bodyParser({KeepExtensions:true,uploadDir:path.join(__ dirname,'/ public/img/user-avatar')})); \t app.use(express.methodOverride()); \t app.use ();); \t app.use(express.static(path.join()); \t app.use(express.static(path.join) (__dirname,'public'))); }); – rokirakan78