2017-06-14 87 views
0

研究員我開發了一個Rest API,我希望在路由不存在的情況下發送自定義消息,而不是默認發送express.js的html消息。就像我搜索的那樣,我找不到一種方法來做到這一點。Express.js處理未擴展的路由

我試圖做的:

app.all("*",function(req,res){ 
    res.status(404) 
    res.header("Content Type","application/json") 
    res.end(JSON.stringify({message:"Route not found"})) 
    }); 

但是它匹配所有已實現的方法。我只想要未被修改的一個被我的應用程序處理。

編輯1

對於我創建具有下列內容的單獨的文件中的每個enndpoint:例如。 myendpoint.js

module.exports=function(express){ 

    var endpoint="/endpoint" 

    express.get(endpoint,function(req,res){ 
     res.end("Getting data other message") 
    }).post(endpoint.function(req,res){ 
     res.end("Getting data other message") 
    }).all(endpoint,function(req,res){ 
     res.status(501) 
     res.end("You cannot "+res.method+" to "+endpoint) 
    }) 
} 

一個在我的主文件我用:

var endpoint=require('myendpoint.js') 
var MyEndpointController=endpoint(app) 
app.all("*",function(req,res){ 
    res.status(404) 
    res.header("Content Type","application/json") 
    res.end(JSON.stringify({message:"Route not found"})) 
}); 
+0

請確保您的app.all('*'...路由是您最後定義的,因爲Express將按照您定義的順序匹配所有路徑,因此,如果此路線是第一條路線將匹配每一個請求,將它保留到最後將首先嚐試匹配每個以前的路由,然後再默認爲* –

+0

https://stackoverflow.com/questions/44537806/nodejs-shows-error-cannot-get-test/訪問以上並幫助我解決此問題 –

回答

1

你的路線1.Declare所有

2.Define無與倫比的路由請求錯誤respose 在年底

這你必須在應用程序中設置它。 (app.use)不在路線中。

Server.js

//Import require modules 
var express = require('express'); 
var bodyParser = require('body-parser'); 

// define our app using express 
var app = express(); 

// this will help us to read POST data. 
app.use(bodyParser.urlencoded({ extended: true })); 

app.use(bodyParser.json()); 

var port = process.env.PORT || 8081;  

// instance of express Router 
var router = express.Router(); 

// default route to make sure , it works. 
router.get('/', function(req, res) { 
    res.json({ message: 'hooray! welcome to our api!' }); 
}); 

// test route to make sure , it works. 
router.get('/test', function(req, res) { 
    res.json({ message: 'Testing!' }); 
}); 


// all our routes will be prefixed with /api 
app.use('/api', router); 

// this is default in case of unmatched routes 
app.use(function(req, res) { 
// Invalid request 
     res.json({ 
     error: { 
      'name':'Error', 
      'status':404, 
      'message':'Invalid Request', 
      'statusCode':404, 
      'stack':'http://localhost:8081/' 
     }, 
     message: 'Testing!' 
     }); 
}); 

// state the server 
app.listen(port); 

console.log('Server listening on port ' + port); 

請注意:我有前綴 '/ API' 在我的路線。

請嘗試http://localhost:8081/api

你會看到 '{ 「消息」: 「萬歲,歡迎到我們的API!」}'

當您嘗試http://localhost:8081/api4545 - 這是不是一個有效的途徑

你會看到錯誤信息。

1

不能發佈爲註釋(信譽太低......)但是你定義的所有其他路徑之後,這條路呢?

訂單非常重要,您應該先定義您的所有路線,然後再訂購。

1

首先,您需要定義所有現有路線,最後您必須定義沒有 路線。順序是非常重要的

// Defining main template navigations(sample routes) 

app.use('/',express.static(__dirname + "/views/index.html")); 
app.use('/app',express.static(__dirname + "/views/app.html")); 
app.use('/api',express.static(__dirname + "/views/api.html")); 
app.use('/uploads',express.static(path.join(__dirname, 'static/uploads'))); 

//If no route is matched by now, it must be a 404 

app.use(function(req, res, next) { 
    res.status(404); 
    res.json({status:404,title:"Not Found",msg:"Route not found"}); 
    next(); 
});