1

I have two situations to get data from DB

  1. To show normal data

    http://exampleapp.com/task/{{taskId}}
    
  2. To edit data via posting

    http://exampleapp.com/task/{{taskId}}/?state={{app.state}}
    

Both url have the same http://exampleapp.com/task/{{taskId}} just a little bit different with last phrase ?state={{app.state}}

I use Express routing as followed:

app.get('/task/:taskId/(?state=:status(pending|cancel|confirmed|deleted))?', routes.task.show);

But I dont know why it does not work ?

For example error: Cannot GET /task/51d2c53f329b8e0000000001 when going to h**p://exampleapp.com/task/51d2c53f329b8e0000000001

2
  • Can you give a little more details on what you're seeing exactly? I assume you actually have two routes, one for each of the cases you need to support? Can you show both? Does one of them work? Commented Jul 2, 2013 at 16:04
  • After setting router above, when navigating to localhost:3000/task/51d2c53f329b8e0000000001, I got error Cannot GET /task/51d2c53f329b8e0000000001 Because both 2 URLS are nearly the same, so I combine into 1 route Commented Jul 2, 2013 at 16:08

3 Answers 3

2

Query strings cannot be defined in routes. You access query string parameters from req.query.

app.get('/task/:taskId', function(req, res) {
    if (req.query.state == 'pending') { ... }
});

However, if you're modifying a task, this is not the appropriate way to do it. GET requests SHOULD be idempotent: the request SHOULD NOT modify state. That's what POST requests are for.

app.get('/task/:taskId', function(req, res) {
    // show task info based on `req.params.taskId`
});

app.post('/task/:taskId', function(req, res) {
    // set task `req.params.taskId` to state `req.body.state`
});

You could either have a <form> that posts to the task, or make an ajax request:

$.post('/task/1', { state: 'pending' }, function() { ... });
Sign up to request clarification or add additional context in comments.

1 Comment

I modified my code by using put to update after get query string from url and it works now. Anyways thanks for your help
1

According to the Express API, you cannot mix RegExp routes with string routes.

You should do something like this (I'm assuming taskId is an integer):

app.get(/^\/task/([0-9]+)/(?state=:status(pending|cancel|confirmed|deleted))?, routes.task.show);

However, I don't see why you cannot only check if req.query.state is defined in your route. It's probably less error prone and easier:

app.get("/task/:taskId", function( req, res, next ) {
  if (req.query.state) {
    // Do things
  }

  next();
});

1 Comment

After modifying code by using query state and it works as expected. Thanks
0

Your problem is that query strings are not considered in routing. You will either have to redesign your urls (ie, include the state into the url itself, instead of the query string) or check the query string in your route handler function.

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.