2017-09-02 3 views
0

私は非常に奇妙な問題に直面しています。私は2つのルートを設定しました。 1つはダッシュボード用、もう1つは外部API用です。ノードエクスプレスルートの問題

dashboard = require('./routes/dashboard')(passport); 
    api = require('./routes/api'); 

    app.use('/', dashboard); 
    app.use('/api', api); 

次の2つのルートは、api.jsルートファイルで定義されています。

router.post('/somepostlink',function(){ 
    // this is reachable from request query. 
}) 

router.get('/somegetlink',function(){ 
    // this is NOT reachable from request query. 
}) 

私はこれが私のdashboard.jsルートファイルで定義されている:

何のルートが一致していない場合、コントロールは今

router.get('*',function (request,response) { 
console.log("Route not found"); 


return response.send("OOPs :(\nSeems like the page you are looking 
for, isn't available with us.").status(404); 

の下のコードのブロックに転送される// POST localhostへのリクエスト:3000/api/somepostlinkが動作します。 しかし、ローカルホストに要求をGET:3000/API/somegetlinkは

"あなたが を探しているページのような\ nSeems :(おっと、私たちでは利用できません"

+0

は(キャッチに 'app'があるファイル内のすべての' * 'ルートを入れて、' app.getを行います'*'、...) 'setuした後あなたのAPIとダッシュボードのルータ。 – Li357

+0

@AndrewLi私はそれを試しましたが、それでもdashboard.jsルートファイルでそれを見つけることを試み、ダッシュボードのホームページを表示します...( '/')ルート – Nikhil

答えて

0
を示しルータで

、順序が重要です。場所であなたのルーターを変更します。そして、変更キャッチ404エラーハンドラ。

app.use('/api', api); 
app.use('/', dashboard); 

app.use(function(req, res, next) { 
var err = new Error('Not Found'); 
err.status = 404; 
next(err); 
}); 
+0

で処理されたものがそれを試みました...動作しません – Nikhil

関連する問題