2

How can I do this type of thing on node/express

app.get("/users/:id/state/:state", (req, res) => {
      if (req.params.state== 'Published') {
            //do somehting
      } else {
            //do something
      }
});

but filtering by state? Exampe, I wanna have this type of route /users/123/posts?state=published, how do I have to hendle it on node?

1 Answer 1

1

In express, URL query strings do not need to be specified in the route. Instead, you access them using req.query:

app.get("/users/:id/posts", (req, res) => {
    if (req.query.state  == 'published') {
      console.log("published");
    } else {
      console.log("not published");
    }
});

This will process the url: /users/123/posts?state=published

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.