f1nn
f1nn

Reputation: 7047

NodeJS + Express: how to make similar (by mask) URLs route in a different way?

I'm writing a small group blog. I have expirienced some trouble with a very specific routing in my app. To make it clear I've wrote a simple demo-app:

//pathtest.js
//creating §
var express = require('express');
var app = express();
//setting express app
app.configure(function() {
    app.use("/static", express.static(__dirname + '/static'));
    app.use(express.bodyParser());
});
app.listen(3000);


//serving hashtag requests like /music, /photo, /personal, etc. 
app.get('/music', function(req, res) {
    res.send('music');
});
app.get('/photo', function(req, res) {
    res.send('photo');
});
app.get('/personal', function(req, res) {
    res.send('personal');
});
//... and +20+ more various hashtags

//serving SUBCATEGORIES for hashtags
//e.g.: /music/rock, /photo/digital
app.get('/:hashtag/:subcat', function(req, res) {
    res.send('Subcategory ' + req.params.subcat + ' for hashtag ' + req.params.hashtag);
});


//SINCE THIS LINE IT DOESNT WORK 'CAUSE EVERY OF THE FOLLOWING REQUESTS CAN BE SERVED WITH ONE OF 4 UPPER CASES :(

//serving usernames of our service
//We put it apecially AFTER SERVING HASHTAGS as they have similar mask, first should be checked if it's hashtag
//e.g.: /bob_johnson
app.get('/:user', function(req, res) {
    res.send('Welcome, ' + req.params.user + '!');
});

//serving single post service
//e.g.L: /163728
app.get('/:postid', function(req, res) {
    res.send('This is post #' + req.params.postid);
});

//serving post commenting
//e.g.: /35345345/comment
app.get('/:postid/comment', function(req, res) {
    res.send('Edit post #' + req.params.postid);
});

After line

//SINCE THIS LINE IT DOESNT WORK 'CAUSE EVERY OF THE FOLLOWING REQUESTS CAN BE SERVED WITH ONE OF 4 UPPER CASES :(

none of requests can be served. I suppose it's because every of following cases may be served with upper routes. And really, as I underdtand, there is NO actual difference for Node, serving /bob and /234234, but such URL's shoulb be served separately! One - for showing user profile, second - for showing post. And /23423423/comment should be served as comments for post #23423423, but /bob/comment should be ignored and redirected to /bob. But /music – is not a username, it's a hashtag, and /music/comment - is not a comments for post #music, but subcategory for hashtag music, etc. This list can be continued...

The main question: how to make Node.JS route similar (by mask) URL's in differnt way?

Thanks!

Upvotes: 0

Views: 3582

Answers (1)

Leonid Beschastny
Leonid Beschastny

Reputation: 51480

You can use RegExp's in routes.

Try:

//serving single post service
//e.g.L: /163728
app.get('/:postid(\d+)', function(req, res) {
    res.send('This is post #' + req.params.postid);
});

//serving usernames of our service
//e.g.: /bob_johnson
app.get('/:user', function(req, res) {
    res.send('Welcome, ' + req.params.user + '!');
});

Or:

//serving single post service
//e.g.L: /163728
app.get(/^\/(\d+)$/i, function(req, res) {
    res.send('This is post #' + req.params[0]);
});

//serving usernames of our service
//e.g.: /bob_johnson
app.get('/:user', function(req, res) {
    res.send('Welcome, ' + req.params.user + '!');
});

Upvotes: 2

Related Questions