Generating random search results
To populate our template, we need to generate some random search result data. For this, we can use the Chance.js
library. We will generate random data on the server side, not on client side, so that we can later demonstrate how to make an HTTP request using Angular 2.
Chance.js
is available for both client-side and server-side JavaScript. We earlier downloaded the Chance.js
package to use with Node.js
. Here is the code to generate random data. Place it in the app.js
file above the /*
route so that /*
doesn't override the random data route:
var Chance = require("chance"), chance = new Chance(); app.get("/getData", function(httpRequest, httpResponse, next){ var result = []; for(var i = 0; i < 10; i++) { result[result.length] = { title: chance.sentence(), desc: chance.paragraph() } } httpResponse.send(result); })
Here, we first create a route for the /getData
path, which sends an array of search results...