Node.js Express learning notes

1. Query String

1.1 What is a query string ?

On the World Wide Web, a query string is the part of a uniform resource locator (URL) which assigns values to specified parameters. Ref: Wikipedia page of Query string

For example:

https://en.wikipedia.org/wiki/Query_string

An URL could contain multiple parameters in the query string, they are separated by ampersand, "&", for example:

http://example.com/path/to/page?name=ferret&color=purple

2.2 What is get parsed and unparsed query string, how to get them ?

For example :

URL: http://localhost:300/query_string?user=toto&A=B=3&C=%26&X%20Y=W+Z&X%20Y=W%2BZ

2.2.1 Parsed query string

var app = express();
app.get('/query_string', function(req, res){
    //console.log("unpassed query : " + req._parsedUrl.query);
    console.log("passed query : " + JSON.stringify(req.query));
    let query = req.query;
    let str = '';
    for(x in query){
        str = str + x + ' - ' + query[x] + '<br>'
    }

    res.send('Query string : <br>' + str);
});

{"user":"toto","A":"B=3","C":"&","X Y":["W Z","W+Z"]}

2.2.2 Unparsed query string

var app = express();
app.get('/query_string', function(req, res){
    console.log("unpassed query : " + req._parsedUrl.query);
    //console.log("passed query : " + JSON.stringify(req.query));
    let query = req.query;
    let str = '';
    for(x in query){
        str = str + x + ' - ' + query[x] + '<br>'
    }

    res.send('Query string : <br>' + str);
});
user=toto&A=B=3&C=%26&X%20Y=W+Z&X%20Y=W%2BZ