2016-03-15 64 views
1
的NodeJS工作

我在我的網站封鎖'api', 'components', 'app', 'bower_components', 'assets'如下:爲什麼正則表達式是不是在

app.route('/:url(api|components|app|bower_components|assets)/*') 
    .get(errors[404]); 

不幸的是,我能夠訪問時,我在我的網站鍵入「http://localhost/api」。請讓我知道上面的代碼中哪一個缺失或錯誤。謝謝。

+2

':url'是做什麼用的? – hjpotter92

+0

@ hjpotter92所提供的語法URL – ppshein

+0

是正確的,你也可以嘗試添加一個斜線,例如'「/:URL \ /(API |成分|應用| bower_components |資產)/ *」'(請注意,反斜槓可能需要加倍)。 –

回答

-1

你的例子是工作,嘗試用http://localhost:3000/api 來訪問你的應用我做了一個功能例如:

var express = require('express'); 
var app = express(); 


app.route('/:url(api|components|app|bower_components|assets)/*') 
    .get(function blockRoutes(req, res, next){ 
    console.log('req.params.url: ',req.params.url); 
    res.status(404).send('not found'); 
    }); 

app.get('/api', function (req, res) { 
    res.send('API accessed!'); 
}); 

app.listen(3000, function() { 
    console.log('Example app listening on port 3000!'); 
}); 

測試這個例子與 http://localhost:3000/api //如何返回200 http://localhost:3000/api/something //如何返回404

+0

創建示例:[email protected] –