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] ...

Get JavaScript: Moving to ES2015 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.