Skip to content

Instantly share code, notes, and snippets.

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

Networked Media Week 5 Assignment

Craig Pickard

I followed the instructions to create a simple web server using Servi. The examples of server_v1.js and server_v2.js were good introductions for understanding the use of Route(), but I ran into some trouble when I was trying to access data inside of the object that I created.

The basic premise of the site is that it's a page for a sports team. I'm an avid football (soccer) supporter, so I created a VERY rudimentary page for Aresnal FC, who are based in London, England. The site has a simple homepage and a simple about page. I then created a simple HTML template for displaying the player profiles. The javascript file contains a JSON object that stores the data for each player. I then used "route('/profiles/:playerName', showProfiles);" as a way to display each players profile.

At present, the server is not working as terminal keeps reporting the following error when i try and run "node server_v3.js":

/root/server_v3.js:57 }); ^ SyntaxError: Unexpected token ) at Module._compile (module.js:439:25) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:906:3

I have gone through the code and can't seem to find the syntactical error anywhere, so for the moment I am stumped. You can view the code in the gists.

*As an additional exercise, I used git from the command line to clone and update my gists.

<!doctype html>
<html land = "en-US">
<head>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<footer>
</footer>
<script src="server_v3.js"></script>
</body>
</html>
<!doctype html>
<html land = "en-US">
<head>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<footer>
</footer>
<script src="server_v3.js"></script>
</body>
</html>
<!doctype html>
<html land = "en-US">
<head>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title><%= player %></title>
</head>
<body>
<h1> Arsenal FC First Team XI </h1>
<h2> <%= name %></h2>
<p>Position: <% = position %> </p>
<p>Squad Number: <% = squadNumber %> </p>
<p>Appearances: <% = FirstXIappearances %></p>
<footer>
</footer>
<!-- <script src="server_v3.js"></script> -->
</body>
</html>
// 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
port(3005);
// start the server
start();
// define the routes
route('/',showHome);
route('/about', showAbout);
route('/profiles/:playerName', showProfiles);
// define the methods
function showHome(request){
request.respond("This is the home page.");
}
function showAbout(request){
request.respond("this is the about page");
}
function showProfiles(request){
request.respond("this is where the player profiles can be found");
}
// Version 2: Define one or more dynamic routes
var servi = require('servi');
var app = new servi(true);
port(3005);
start();
/*
TO DO:
Start with the previous version, with static routes defined, and add dynamic routes
*/
// example dynamic route:
//route('/vegetables/:vegname',myFunction);
route('/',showHome);
route('/about', showAbout);
route('/profiles/:playerName', showProfiles);
// inside the callback function, access the variable ":vegname" with request.params.vegname
// example callback function:
function showProfiles(request){
request.respond("The player's name is: " + request.params.playerName);
}
function showHome(request){
request.respond("This is the home page.");
}
function showAbout(request){
request.respond("this is the about page");
}
// For another example of dynamic routing, see:
// https://github.com/robynitp/networkedmedia/blob/master/10-server-side/01-servi-userprofiles/server.js
var servi = require('servi');
var app = new servi(true);
port(3005);
// example dynamic route:
//route('/vegetables/:vegname',myFunction);
route('/',showHome);
route('/about', showAbout);
route('/profiles/:playerName', showProfiles);
// example data array:
var players = {
Szczesny: {
name: "Wojciech Szczesny",
position: "Goalkeeper",
SquadNumber: "1",
FirstXIappearances: "170"
},
Chambers: {
name: "Callum Chambers",
position: "Defender",
SquadNumber: "21",
FirstXIappearances: "19"
}
};
function showProfiles(request){
var player = request.params.playerName;
if (player in players){
var name = players[player].name;
var position = players[player].position;
var squadNo = players[player].SquadNumber;
var appearances = players[player].FirstXIappearances;
request.render("profileTemplate.html", players[player]);
} else {
request.respond("<p> Player does not exist </p>");
}
}
function showHome(request){
request.respond("This is the home page.");
}
function showAbout(request){
request.respond("this is the about page");
}
start();
<!doctype html>
<html land = "en-US">
<head>
<meta charset = "utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<footer>
</footer>
<script src="server_v3.js"></script>
</body>
</html>
@robynitp
Copy link

robynitp commented Dec 1, 2014

This is looking good. Please get in touch if you're still having trouble, and I can help you debug.

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