Skip to content

Instantly share code, notes, and snippets.

@marsimaria
Forked from robynitp/README.md
Last active August 29, 2015 14:10
Show Gist options
  • Save marsimaria/7d4ed93c553e2d43211f to your computer and use it in GitHub Desktop.
Save marsimaria/7d4ed93c553e2d43211f to your computer and use it in GitHub Desktop.

Networked Media Week 5 Assignment

I continued my marijuana word cout project and created three pages. I didn't have time to figure out how to call my css stylesheet in servi, so I left it out. It looks very raw but the functions are working.

The first page shows the title of a webpage. The second one is just for fun. It lets you type in any keyword to show what you smoke. The third one shows snippets of three randomly chosen articles that have the word marijuana in its designated year.

// Version 1: Define some static routes
// a server that defines some static routes
// every servi application must have these 2 lines
var servi = require('servi');
var app = new servi(true);
// set the port (defaults to 3000 if you leave out this line)
port(3001);
// start the server
start();
// define the first route -- home page
route('/',showHome);
route('/about', aboutPage);
route('/author', authorPage);
//route('year/:titles', showYear);
// define the showHome method
function showHome(request){
request.respond("<h1>Marijuana Articles Count</h1>");
}
function aboutPage(request) {
request.respond("<h4>New York Times</h4>");
}
/**
function showYear(request) {
var content = "<h1>" + request.params.titles + "</h1>";
content += "<p>Top Ten Titles</p>"
request.respond(content);
}
**/
function authorPage(request) {
var content = "<h1>Author: Bongo Fang</h1>";
content += "<img src='photo.jpg'>";
request.respond(content);
}
// TO DO:
// define 2+ more routes with corresponding functions
// Version 2: Define one or more dynamic routes
var servi = require('servi');
var app = new servi(true);
port(3002);
start();
/*
TO DO:
Start with the previous version, with static routes defined, and add dynamic routes
*/
// example dynamic route:
route('/smoking/:stuff',getStuff);
// inside the callback function, access the variable ":vegname" with request.params.vegname
// example callback function:
function getStuff(request){
request.respond("I'm smoking: " + request.params.stuff);
}
// For another example of dynamic routing, see:
// https://github.com/robynitp/networkedmedia/blob/master/10-server-side/01-servi-userprofiles/server.js
/*
Note:
In your version, the function should not be called "myFunction". Modify it and name it something meaningful.
Function names are often verbs, like "getVegetable", "showProfilePage", "processForm", etc.
*/
// 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 &#8212; 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);
}
@robynitp
Copy link

robynitp commented Dec 1, 2014

This is looking good. You might think about making the years variable an object instead of an array. That way you could easily pull in the data for each year using a property. For example:

var years = {
     1970: {stuff:etc},
     1980: {stuff:etc},
     1990: {stuff:etc}
};

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:

var years = [];
years[1970] = {stuff:etc};
years[1980] = {stuff:etc};
years[1990] = {stuff:etc};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment