0

I have a Node.js server which is handling post methods like this:

app.post('/app/:id:second',function(req,res){
  console.log('Post received');
  res.end();
})

How can I pass 2 or more parameters to my server in a URL? How should the url look like? I tried it like this but it failed: http://localhost:8080/app/id=123&second=333
I'm a beginner in web apps.

3
  • Url params !== query params. Commented Feb 11, 2015 at 15:09
  • then how can I send the params to my server? I have 2 forms and I'm trying to determine which one was pressed. Commented Feb 11, 2015 at 15:14
  • Does the answer on this similar question help? stackoverflow.com/questions/22223868/… Commented Feb 11, 2015 at 15:24

2 Answers 2

1

Use bodyParser midleware, for example:

var bodyParser= require('body-parser');
app.use(bodyParser.urlencoded());

app.post('/app/:id',function(req,res){  //http://localhost:8080/app/123?second=333

  console.log(req.query); //{second: 333}
  console.log(req.params); // {id: 123}
  res.end();
})
Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to form the url to that both "id" and "second" are in req.params?
If you want 'second' in params you must write somthing like that: app.post('/app/:id/:second',function(req,res).... localhost:8080/app/123/333 But this not correct!!!
This is the situation I'm trying to figure out and please tell me what the right solution is. When a person clicks on a link like this: localhost:8080/app/12345 two web forms should be visible. Now, when that person clicks on the web form button a POST method should be executed. I need to know which button was pressed so that I can handle it in the app.post method on my server. That is why I'm trying to send 2 parameters, the "ID"(which is 12345) and a "BUTTON PRESSED" parameter. Is your answer good for my problem?
If that was my problem, i would use my answer =)
0
app.post('/app',function(req,res){
  console.log(req.query.id);
  console.log(req.query.second);
  res.end();
})

Note: -req url should be like this :- http://localhost:8080/app?id=123&second=333

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.