0

I'm using node.js 0.10.22 and express 3.4.6

I would like to send something like this /upload?slides=2 and then get the value for slides

I'm using the bodyParser middleware.

I have this:

app.get('/',function(req, res) {
    // show a file upload form
    res.writeHead(200, {'content-type': 'text/html'});
    res.end(
        '<form action="/upload?slides=2" enctype="multipart/form-data" method="post">'+
        '<input type="text" name="title"><br>'+
        '<input type="file" name="upload" multiple="multiple"><br>'+
        '<input type="submit" value="Upload">'+
        '</form>'
    );
});

app.post('/upload',function(req,res){
    doLog("/upload","hit upload");
    //console.log(req);
    console.log("params = ");
    console.log(req.body);
    ...

In the console I just get {title: ''}

Or do I have to put the parameters elsewhere in the form?

  • 2
    try `console.dir(req)` in the app.post() – rdodev Dec 27 '13 at 21:00
  • @rdodev that didn't change anything. –  Dec 27 '13 at 21:01
  • So what is `slides`, there are no elements with that name? Are you trying to get the files ? – adeneo Dec 27 '13 at 21:01
  • @adeneo no, I'm not trying to get the files. I'm trying to send extra information not contained in the files I want to upload –  Dec 27 '13 at 21:03
  • @Houseman of course that doesn't change anything functionally, but it should output of the req structure, which is what you want to see. – rdodev Dec 27 '13 at 21:03
  • Then add hidden form fields, you're not doing a GET request, but a POST request, you can't just add stuff to the querystring. – adeneo Dec 27 '13 at 21:04
  • @rdodev I meant that I still get the same output. But then I realized that I wrote it wrong, and now I get a giant object –  Dec 27 '13 at 21:05
  • @adeneo Okay, I'll add hidden form fields. I didn't know that I couldn't just add stuff to the querystring –  Dec 27 '13 at 21:06
  • I think someone else already asked this question here: http://stackoverflow.com/questions/5710358/how-to-get-post-query-in-express-node-js – rdodev Dec 27 '13 at 21:06
  • @rdodev I tried the solutions in that answer first. I got it to work using a slightly different method, which I posted as an answer –  Dec 27 '13 at 21:09

1 Answers1

0

I got it to work after using

console.dir(req). From the output of that, I saw an object called query with my parameters inside.

Then I called console.log(req.query.slides) and it returned 2, which was what I wanted.

  • So, basically you used what I suggested in the comments and decided to post an answer by yourself? – rdodev Dec 27 '13 at 21:17
  • @rdodev You didn't say anything about `req.query`. You only gave me `console.dir`, which I upvoted you for. The people who answered the similar question that you linked to didn't say anything about `req.query` either. In fact, the asker tried using `req.query`, and it wouldn't work for him. –  Dec 27 '13 at 21:21
  • I suggested you use `console.dir(req)` and you immediately dismissed me saying that "didn't change anything". I had to explain to you that what you want to see is the structure of `req` because that's where your likely answer was, given it wasn't in `req.body`. That's when you saw that `req.query` had the contents you wanted. Failing to recognize that you wouldn't have gotten that far without those tips is your prerogative. Usually in this site when someone helps you, it's good etiquette to have them submit an answer and accept it. Anyhow, your issue is now solved and that's what matters. – rdodev Dec 27 '13 at 21:41
  • @rdodev I followed that immediate dismissal with `"...I realized that I wrote it wrong, and now I get a giant object"`. I knew I wouldn't have gotten this far without your tip. But that's what it was. A tip. That's why I gave you an upvote. You didn't give me the answer, you only helped me to reach it. If you would have mentioned `req.query`, I would have asked you to make an answer out of it. –  Dec 27 '13 at 22:15