Thursday 4 May 2017

How routing works in Express with optional parameters?

At the moment I've routing like this:

app.get('/post/:id?/:edit?/:add?') // ... func and something else

When I'm sending query to url '/post/', without parameters, it retrieves all posts.

On '/post/5', where 5 is id of post, it retrieves post with id 5 in db.

On '/post/5/edit', opens page with form, for editing current post.

Everything is good. But when I'm trying to open 'post/add/, it didn't work., because req.params shows in console that: id: 'add', edit: undefined, add: undefined. How to fix it?



via Ted Watkins

No comments:

Post a Comment