Ask*_*ken 2 javascript wildcard node.js
我在使用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');
});
}
Run Code Online (Sandbox Code Playgroud)
启动应用程序(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);
Run Code Online (Sandbox Code Playgroud)
我想要做的是:
app.get('/data*', function(...
Run Code Online (Sandbox Code Playgroud)
我通过解析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');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5582 次 |
| 最近记录: |