最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - NodeJS setting up wildcard routes or url rewrite - Stack Overflow

programmeradmin0浏览0评论

I have a simple route thing like this in node using 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');
    });
}

Starting the app (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);

What I want to be able to do is:

app.get('/data*', function(...

I determine what data to return by parsing the url.

I have a simple route thing like this in node using 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');
    });
}

Starting the app (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);

What I want to be able to do is:

app.get('/data*', function(...

I determine what data to return by parsing the url.

Share Improve this question edited Nov 18, 2011 at 11:51 Reporter 3,9485 gold badges35 silver badges49 bronze badges asked Nov 18, 2011 at 11:44 AskenAsken 8,08110 gold badges49 silver badges82 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

First of all, the router middleware has been removed from Connect, so you can either use Express or make your own router to be safer for the future ( see this mit: https://github./senchalabs/connect/mit/2ca7ec3ff64cb7600bfd029233228236bf048671 ).

If you choose to use Express, you can pass in a regular expression for the route (more info here: http://expressjs./guide.html#routing), but I would use something like this instead (for your specific case):

app.get('/data/:type', function (req, res) {
  console.log('Received ' + req.params.type + ' data');
});
发布评论

评论列表(0)

  1. 暂无评论