我正在写一个小组博客。我在我的应用程序中遇到了一些非常具体的路由问题。为了清楚起见,我编写了一个简单的演示应用程序:
//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);
});
线后
//SINCE THIS LINE IT DOESNT WORK 'CAUSE EVERY OF THE FOLLOWING REQUESTS CAN BE SERVED WITH ONE OF 4 UPPER CASES :(
无法满足任何请求。我想这是因为以下每种情况都可能使用上层路线。事实上,据我所知,Node 没有实际区别,服务 /bob 和 /234234,但这样的 URL 应该单独提供!一 - 用于显示用户资料,二 - 用于显示帖子。并且 /23423423/comment 应该作为帖子 #23423423 的评论,但 /bob/comment 应该被忽略并重定向到 /bob。但是 /music – 不是用户名,它是主题标签,而 /music/comment – 不是对 #music 帖子的评论,而是主题标签音乐的子类别等。此列表可以继续...
主要问题:如何以不同的方式使 Node.JS 路由相似(通过掩码)URL?
谢谢!