Friday, December 9, 2016

What's In A Query solution and explanations

Directions:
Write a route that extracts data from query string in the GET '/search' URL route (e.g. ?results=recent&include_tabs=true) and then outputs it back to the user in JSON format.
Code:
var express = require('express')
var app = express(); // make “app” an instance of express
                 app.get('/search', function (req, res) {  // The ol’ “app.method(path.handler)” format
                 var result = req.query // Use “req.query” on the query string that comes in from the browser, and find out what the parameters of the query are, and return just those
                 res.json(result) // send back the result to the browser in JSON format --- could also have used “res.send(result),” I guess
}) // end of GET statement
app.listen(process.argv[2]) //OK, get the ball rolling by listening for requests here

OUTPUT:
{
"results":"recent",
"type":"quote",
"page":"5"

}

No comments:

Post a Comment