[英]NodeJS setting up wildcard routes or url rewrite
我在使用connect(source / routes.js)的節點中有一個這樣的簡單路由:
exports.routes = function(app) {
app.get('/data', function(req, res, params) {
res.writeHead(200, { 'Content-type': 'text/plain' });
res.write('Authenticated: ' + connect.session.auth + '\n');
res.end('app.get /data');
});
}
啟動應用程序(app.js):
var routes = require('connect');
var routes = require('./source/routes');
var server = connect.createServer(
connect.cookieParser(),
connect.session({ secret: 'justmeknowsthis', cookie: { maxAge: config.data.sessionTimeout }}),
connect.router(routes.routes)
);
server.listen(3000);
我想要做的是:
app.get('/data*', function(...
我通過解析url來確定要返回的數據。
首先,路由器中間件已從Connect中刪除,因此您可以使用Express或使您自己的路由器在未來更安全(請參閱此提交: https : //github.com/senchalabs/connect/commit/2ca7ec3ff64cb7600bfd029233228236bf048671 )。
如果您選擇使用Express,您可以傳遞路線的正則表達式(更多信息請訪問: http : //expressjs.com/guide.html#routing ),但我會使用類似的東西(針對您的具體情況) ):
app.get('/data/:type', function (req, res) {
console.log('Received ' + req.params.type + ' data');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.