0

I try to parse a request with node.js, So I build the next:

 app.get('/download', function(req, res){
      var parseJson = JSON.parse(req);
      var file = parseJson.fileName;
      res.download(file); // Set disposition and send it.
    });

And I send a request with the Advanced Rest Client:

URL: http://localhost:5000/download

Method: GET

Raw arguments: fileInfo: "C://1//239698_n.jpg"

and it gives me the next error:

SyntaxError: Unexpected token o
    at Object.parse (native)
    at port (c:\Node\nodejs\express.js:13:24)
    at callbacks (c:\Node\nodejs\node_modules\express\lib\router\index.js:164:37)
    at param (c:\Node\nodejs\node_modules\express\lib\router\index.js:138:11)
    at pass (c:\Node\nodejs\node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (c:\Node\nodejs\node_modules\express\lib\router\index.js:173:5)
    at Object.router (c:\Node\nodejs\node_modules\express\lib\router\index.js:33:10)

What can be the reson?

3
  • 1
    have you tried logging request and confirming it is valid JSON? Commented Apr 22, 2014 at 13:36
  • If you're using express, you can probably just skip the JSON.parse step. I don't think the data you want is in req, but rather req.query Commented Apr 22, 2014 at 13:43
  • That's probably a better answer than mine below, if Express provides a direct way to get at your data then that should be the preferred way to do it Commented Apr 22, 2014 at 14:49

1 Answer 1

1

You're not sending JSON. You're sending querystring formatted arguments. You'll want to use querystring.parse() or node-querystring or, shameless plug alert, my module that is format agnostic, objectifier.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.