Erdi
Erdi

Reputation: 1884

Rewrite URL in Node.js and Express Application

i'm using GET method with a parameter(:slug) to show my post in my website ;

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.

So , i think ,if i change my get method to like this;

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" .

What is your suggestions , thank you .

Versions: Node.js - 0.10.33 , Express - 5.0.0-alpha.1

Upvotes: 0

Views: 205

Answers (1)

Scimonster
Scimonster

Reputation: 33409

I'm going to address your basic issue here instead of your specific question. (What is the XY problem?)

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.

Upvotes: 3

Related Questions