|
// Version 3: Use dynamic data |
|
|
|
// Fill in the necessary code starting code |
|
|
|
/* |
|
TO DO: |
|
Working from your previous version, set up some data in an object or an array, |
|
and use the route variable to pull a particular piece of data from the object. |
|
*/ |
|
|
|
var servi = require('servi'); |
|
var app = new servi(true); |
|
port(3002); |
|
start(); |
|
|
|
route('/year/:year',getSnippets); |
|
|
|
// === DATA EXAMPLES === // |
|
|
|
// example data array: |
|
var years = [ |
|
{ |
|
"year": "1930s", |
|
"snippet": [ |
|
"Government Will Ask States To Ban Growing of Marijuana", |
|
"DENVER, Although as appalling in its effects on the human mind and body as narcotics, the consumption of marijuana appears to be proceeding, virtually unchecked in Colorado and other Western States with a large Spanish-American...", |
|
"Officials of two New York State colleges denied tonight a statement that the 'reefer man' had invaded their campuses.", |
|
] |
|
}, |
|
{ |
|
"year": "1951", |
|
"snippet": [ |
|
"Schoolteachers and others handling teen-age children were asked yesterday to report to the police any signs of narcotic addiction among their pupils.", |
|
"One out of every 200 high school boys and girls in this city probably is a user of narcotic drugs, Superintendent of Schools William Jansen conceded yesterday at an open hearing conducted by Attorney General Nathaniel Goldstein.", |
|
"Chronic users of barbiturate sleeping pills are more likely to become drug addicts than are persons who smoke marijuana or take cocaine." |
|
|
|
] |
|
}, |
|
{ |
|
|
|
"year": "1966", |
|
"snippet": [ |
|
"An undisclosed number of students at the Washington Square complex of New York University have been ordered out of a dormitory as a result of an inquiry by the university and the police into reported use of marijuana.", |
|
"6 of 9 Penna State U students arrested in Feb marijuana raid plead guilty, fined $500 each", |
|
"Seven persons were arrested in a Greenwich Village apartment early yesterday after detectives said they found four ounces of opium, three ounces of marijuana and 30 bottles of barbiturates on the premises." |
|
] |
|
}, |
|
{ |
|
|
|
"year": "1970", |
|
"snippet": [ |
|
"Narcotics arrests in New York City increased sharply during 1969, according to the Police Department.", |
|
"Hunter College's assembly hall was filled to capacity last night to hear several speakers denounce the verdicts at the trial of the Chicago 7 and condemn the charges here against 13 Black Panthers.", |
|
"The morale of the professional American fighting man has been shaken by the frustrations of Vietnam, a wave of antimilitarism in the United States, race troubles, drugs and reverses and scandals in individual services like the Sonmy affair." |
|
] |
|
}, |
|
{ |
|
|
|
"year": "2013", |
|
"snippet": [ |
|
"New research suggests that vermicompost, a worm-created soil additive, helps plants grow with more vigor, and makes them more resistant to disease and insects, than those grown with other types of composts and fertilizers.", |
|
"The photographer Henry Grossman is releasing “Places I Remember,” a new book of photographs of the Beatles.", |
|
"With reggae music pumping in the background and flashing disco-style lights, members of the recreational pot club lit up in celebration of the new year — and a new place to smoke legally among friends." |
|
] |
|
} |
|
|
|
] |
|
|
|
// you could get the index number of the array from a route parameter variable |
|
// for example: |
|
|
|
function getSnippets(request) { |
|
var content = "<h4>" + years[request.params.year]["year"] + " Snippets of Articles</h4>"; |
|
for(var i=0; i < years[request.params.year]["snippet"].length; i++) { |
|
content += "<p>" + years[request.params.year]["snippet"][i] + "</p>" |
|
} |
|
request.respond(content); |
|
} |
This is looking good. You might think about making the
yearsvariable an object instead of an array. That way you could easily pull in the data for each year using a property. For example:Actually, since these are numbers and not strings, you also have the option of using the years as index numbers in an array, like this: