[英]Rewrite URL in Node.js and Express Application
i'm using GET method with a parameter(:slug) to show my post in my website ; 我正在使用带有参数(:slug)的GET方法在我的网站上显示我的帖子;
app.get('/:slug', function(req, res) {
var slug = req.param("slug");
//other codes...
}
if i click "domain.com/pink-car" , app.get('/:slug'
works and response to "pink car" post from mongodb but i also have other GET methods like /login /admin
, if i enter these , app.get('/:slug'
fired again. 如果我单击“ domain.com/pink-car”,则
app.get('/:slug'
可以正常工作,并响应来自mongodb的“ pink car”帖子,但是如果我输入这些,我也可以使用其他GET方法,例如/login /admin
, app.get('/:slug'
再次触发。
So , i think ,if i change my get method to like this; 因此,我想,如果我将get方法更改为这样;
app.get('/posts/:slug', function(req, res) {
}
In this time , my url will be "domain.com/posts/pink-car" , but for the SEO , i want to reWrite this url to "domain.com/pink-car" . 这一次,我的网址将是“ domain.com/posts/pink-car”,但对于SEO,我想将此网址重新写入“ domain.com/pink-car”。
What is your suggestions , thank you . 您有什么建议,谢谢。
Versions: Node.js - 0.10.33 , Express - 5.0.0-alpha.1 版本:Node.js-0.10.33,Express-5.0.0-alpha.1
I'm going to address your basic issue here instead of your specific question. 我将在这里解决您的基本问题,而不是您的特定问题。 ( What is the XY problem? )
( 什么是XY问题? )
As long as your routes are defined in the proper order, it should work. 只要您的路线以正确的顺序定义,它就可以正常工作。 So, first define
/login
and /admin
, and only then do /:slug
, and it should all work out fine. 因此,首先定义
/login
和/admin
,然后再执行/:slug
,它应该都可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.