azat-co / expressworks

Learn Express.js from the author of one of the best books on Express.js—Pro Express.js— with this workshop that will teach you basics of Express.js.
MIT License
709 stars 220 forks source link

Official WHAT'S IN QUERY solution doesn't work #82

Closed occult closed 8 years ago

occult commented 8 years ago

The official solution is:

    var express = require('express')
    var app = express()

    app.get('/search', function(req, res){
      var query = req.query
      res.send(query)
    })

    app.listen(process.argv[2])

but it doesn't work.

My solution was:

var express = require('express');
var app = express();

app.get('/search', function (req, res) {
  var query = req.query;
  delete query.__proto__;
  res.send(JSON.stringify(query));
});

app.listen(process.argv[2]);

I don't know if it's a good solution, but it worked.

ghost commented 8 years ago

The official solution worked fine for me. Did you completely paste it to see for yourself?

azat-co commented 8 years ago

delete query.__proto__; is the only difference?

ysahbi commented 8 years ago

The official solution didn't work for me neither.

ghost commented 8 years ago

Here's what I got with the Official Solution:

             ACTUAL                                 EXPECTED                
───────────────────────────────────────────────────────────────────────────────

   "{"                                 !=    "{\"results\":\"recent\",\"type\":\"quote\",\"page\":\"10\"}"
   "  \"type\": \"quote\","            !=                                       
   "  \"page\": \"10\","               !=                                       
   "  \"__proto__\": {}"               !=                                       
   "}"                                 !=                                       

────────────────────────────────────────────────────────────────────────────────

I'm guessing it has something to do with my express version. I'm using 3.2.6 at the moment. Deleting proto and using JSON.stringify worked like a charm!