diff --git a/week6/00-servi-static-files/faq.html b/week6/00-servi-static-files/faq.html new file mode 100644 index 0000000..3056ec7 --- /dev/null +++ b/week6/00-servi-static-files/faq.html @@ -0,0 +1,18 @@ + + +
+Q: What is Servi?
+A: servi.js is a JavaScript node.js microframework for creating web servers. It is inspired by Processing and p5.js.
+Q: Where do I find documentation?
+A: For some Servi basics, see the servi.js github wiki
+Q: How do I do this in Servi?
+A: For more info on how to do this assingment, see Using Servi in the Networked Media class wiki
+ + \ No newline at end of file diff --git a/week6/00-servi-static-files/server.js b/week6/00-servi-static-files/server.js new file mode 100644 index 0000000..0bb94ec --- /dev/null +++ b/week6/00-servi-static-files/server.js @@ -0,0 +1,30 @@ +// 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); // make sure "port()" line comes before "start()" +start(); +// set up the routes +route('/',showHome); +route('/about',myAboutPage); +route('/faq',viewFaq); + +function showHome(request){ + // serve a simple string + request.respond("This is the home page."); +} + +function myAboutPage(request){ + // build a string with some HTML in it + var myHtml = "I am a person from earth.
"; + // serve the HTML string + request.respond(myHtml); +} + +function viewFaq(request){ + // serve a static HTML file + request.serveFile('faq.html'); +} + diff --git a/week6/01-servi-userprofiles/server.js b/week6/01-servi-userprofiles/server.js new file mode 100644 index 0000000..dc4fc34 --- /dev/null +++ b/week6/01-servi-userprofiles/server.js @@ -0,0 +1,27 @@ +// 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(3111); // make sure "port()" line comes before "start()" +start(); +// set up the routes +route('/',showHome); +route('/about',myAboutPage); + +function showHome(request){ + request.respond("This is the home page."); +} + +function myAboutPage(request){ + request.respond("All the user's info here
"; + request.respond(content); +} + diff --git a/week6/02-servi-userprofiles-data/server.js b/week6/02-servi-userprofiles-data/server.js new file mode 100644 index 0000000..574d7d8 --- /dev/null +++ b/week6/02-servi-userprofiles-data/server.js @@ -0,0 +1,44 @@ +// 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(7777); //make sure this comes before "start()" +start(); +// set up the routes +route('/',showHome); +route('/profile/:userid',showProfile); + +var users = [ + { + "name": "Joe", + "about": "I like cats and kittens." + }, + { + "name": "Jane", + "about": "I live in Alberta and like to code." + }, + { + "name": "Morgan", + "about": "Craft beer and pickles and kale!" + } +]; + +function showHome(request){ + request.respond("This is the home page."); +} + +function showProfile(request){ + var id = request.params.userid; + if (id in users){ + var name = users[id].name; + var about = users[id].about; + var content = "" + about + "
"; + request.respond(content); + } else { + request.respond("Could not find that user id
"); + } +} + + diff --git a/week6/03-servi-form-get/form.html b/week6/03-servi-form-get/form.html new file mode 100644 index 0000000..7b22c79 --- /dev/null +++ b/week6/03-servi-form-get/form.html @@ -0,0 +1,12 @@ + + + + +Return home
+ + \ No newline at end of file diff --git a/week6/09-servi-db-search/people.db b/week6/09-servi-db-search/people.db new file mode 100644 index 0000000..4bf486f --- /dev/null +++ b/week6/09-servi-db-search/people.db @@ -0,0 +1,7 @@ +{"name":"Dan","date_added":"Sun Nov 30 2014 22:00:04 GMT-0500 (EST)","_id":"DGW46VG8N1kCsOXH"} +{"name":"Annabelle","date_added":"Sun Nov 30 2014 21:59:26 GMT-0500 (EST)","_id":"S0hQsabVo80Rw7Ln"} +{"name":"Julia","date_added":"Sun Nov 30 2014 21:59:07 GMT-0500 (EST)","_id":"jG2eAyNxMUrCbzqp"} +{"name":"Diego","date_added":"Sun Nov 30 2014 21:56:30 GMT-0500 (EST)","_id":"t5GLbvx9qyRiJHW7"} +{"name":"Janet","date_added":"Sun Nov 30 2014 21:56:15 GMT-0500 (EST)","_id":"yaZf0BFknoNXYcVA"} +{"name":"Ziggy","date_added":"Sun Nov 30 2014 22:01:33 GMT-0500 (EST)","_id":"t32vvLCPk8xYXTwD"} +{"name":"Dan","date_added":"Sun Nov 30 2014 22:01:51 GMT-0500 (EST)","_id":"WR9a7FDWcEapmXB3"} diff --git a/week6/09-servi-db-search/server.js b/week6/09-servi-db-search/server.js new file mode 100644 index 0000000..9a0bcc8 --- /dev/null +++ b/week6/09-servi-db-search/server.js @@ -0,0 +1,90 @@ +var servi = require('servi'); +var app = new servi(true); +port(3003); +start(); +// set up a database +// looks for a file called "people.db" or creates one if it doesn't exist +var namesDB = useDatabase("people"); + +// set up the routes +route('/', showAll); +route('/add/:person',addName); +route('/view/:id',getById); +route('/search',doSearch); + +// Add new name to database +// this works from a URL, but could use a form instead, as the search function does below +function addName(request){ + var d = new Date(); // get the current date + var dateToday = d.toString(); // turn it to a human-readable string + namesDB.add( + { + name:request.params.person, + date_added: dateToday + // using a form, you could access more than one parameter + // and put more info into this object + // for example, include city, email address, favorite color, etc. + // city: request.params.city + }); + request.respond("Added "+request.params.person); +} + +// show all the names +function showAll(request){ + namesDB.getAll(gotNames); + function gotNames(names){ + namesText = ""; + if (names.length > 0){ + for (i =0; i < names.length; i++) { + // create a link like: JoeSearch'; + request.respond( namesText + footerText); + } +} + +function getById(request){ + var id = request.params.id; + // get via the built-in _id and send result to displayPerson function + namesDB.getOne(id, displayPerson); + + function displayPerson(personObj){ + console.log(personObj); //the object returned + var content = '
Joined ' + personObj.date_added + '
'; + request.respond(content); + } +} + +function doSearch(request){ + // if the form has not been submitted, show the form + if (typeof request.params.submit === 'undefined'){ + // no params present, so this must be a new search -- show the form. + request.serveFile('html/searchform.html'); + } else { + var searchTerm = request.params.searchname; + + namesDB.search('name', searchTerm, foundIt); + + function foundIt(data) { + var responseText = ''; + // if records were found + if (data.length > 0){ + responseText = 'We found:The temperature in " + city + " was once " + temp + "F
"; + content += ''; + request.respond(content); + }); + + /* === Note: === + These examples use an anonymous function as the 2nd parameter in loadJSON(). + If you prefer, you could instead use a named function, like this: + + loadJSON('weather.json',gotWeather); + function gotWeather(dataObj){ + // do stuff + } + + */ +} + +function fromUrlDemo(request){ + var content = ''; + + // OPTION 2. Load JSON from a URL + var myUrl = "http://api.openweathermap.org/data/2.5/weather?q=London,uk"; + loadJSON(myUrl,function(dataObj){ + // get data points from the object + var temp = dataObj.main.temp; + var city = dataObj.name; + + console.log("loaded from URL"); + content += "The temperature in " + city + " right now is " + temp + "K
"; + content += ''; + request.respond(content); + }); +} + +/* + === + === To work with JSON, add these 2 utility functions in your script === + === Then call loadJSON() as in the demos above. + === +*/ + +// load text from a url +function loadURL(url,onSuccess){ + var str = ''; + http.get(url, function(res) { + res.on("data", function(chunk) { + str += chunk.toString(); + }); + res.on("end",function(){ + onSuccess(str); + }); + }).on('error', function(e) { + console.log("Got error: " + e.message); + // use an onError() callback here? + }); +} + +// load JSON from a local file or a URL +function loadJSON(path,callback){ + // is it a URL ? + if (path.indexOf('http') === 0){ + loadURL(path, function(res){ + callback(JSON.parse(res)); + }); + } else { // assume it's a file + callback(JSON.parse(loadFile(path))); + } +} \ No newline at end of file diff --git a/week6/10-servi-load-json/weather.json b/week6/10-servi-load-json/weather.json new file mode 100644 index 0000000..343765f --- /dev/null +++ b/week6/10-servi-load-json/weather.json @@ -0,0 +1,54 @@ + +{ + "coord": { + "lon": -74.01, + "lat": 40.71 + }, + "sys": { + "type": 1, + "id": 1980, + "message": 0.115, + "country": "US", + "sunrise": 1416311222, + "sunset": 1416346537 + }, + "weather": [ + { + "id": 500, + "main": "Rain", + "description": "light rain", + "icon": "10n" + }, + { + "id": 701, + "main": "Mist", + "description": "mist", + "icon": "50n" + }, + { + "id": 300, + "main": "Drizzle", + "description": "light intensity drizzle", + "icon": "09n" + } + ], + "base": "cmc stations", + "main": { + "temp": 47.84, + "pressure": 1000, + "humidity": 93, + "temp_min": 42.8, + "temp_max": 57.2 + }, + "wind": { + "speed": 3.24, + "deg": 270 + }, + "clouds": { + "all": 90 + }, + "dt": 1416267540, + "id": 5128581, + "name": "New York", + "cod": 200 +} \ No newline at end of file diff --git a/week6/11-servi-file-uploads/form.html b/week6/11-servi-file-uploads/form.html new file mode 100644 index 0000000..b4e4063 --- /dev/null +++ b/week6/11-servi-file-uploads/form.html @@ -0,0 +1,11 @@ + + +