Skip to content

Commit

Permalink
more example cleanup and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shiffman committed Nov 10, 2015
1 parent 28d33a7 commit f0a1a69
Show file tree
Hide file tree
Showing 11 changed files with 1,259 additions and 592 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,4 +3,5 @@ _site/
keys.txt
npm-debug.log
config.js
node_modules

1 change: 1 addition & 0 deletions week9/02_serve_files_express/server.js
Expand Up @@ -9,6 +9,7 @@ var app = express();

// This is for hosting files
// Anything in the public directory will be served
// This is just like python -m SimpleHTTPServer
// We could also add routes, but aren't doing so here
app.use(express.static('public'));

Expand Down
57 changes: 44 additions & 13 deletions week9/05_concordance_API_express/server.js
@@ -1,34 +1,63 @@
// A2Z F15
// Daniel Shiffman
// Programming from A to Z, Fall 2014
// https://github.com/shiffman/Programming-from-A-to-Z-F14
// https://github.com/shiffman/A2Z-F15

// Using express: http://expressjs.com/
var express = require('express');
// Create the app
var app = express();

// Set up the server
// process.env.PORT is related to deploying on heroku
var server = app.listen(process.env.PORT || 3000, listen);

// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}

// This is for hosting files
// Anything in the public directory will be served
// This is just like python -m SimpleHTTPServer
// We could also add routes, but aren't doing so here
app.use(express.static('public'));

// The 'fs' (file system) module allows us to read and write files
// http://nodejs.org/api/fs.html
// This is how we'll load data
var fs = require('fs');

// And we'll look at all files in the jane austen directory
var files = fs.readdirSync('austen');

// Pulling our concordance object from a separate "module" - concordance.js
var concordance = require('./concordance');

// Read the file as utf8 and process the data
// Notice how this is in a loop to parse all files
for (var i = 0; i < files.length; i++) {
// Note the callback is processFile
fs.readFile('austen/'+files[i], 'utf8', processFile);
}

// How many files have been read?
// This helps us know when we are done
var fileCount = 0;

// An object that acts as dictionary of words and counts
var wordcounts = new concordance.Concordance();

// This callback is triggered for every file
function processFile(err, data) {
// If there's a problem
if (err) {
console.log('ooops, there was an error reading this file');
throw err;
}

// Send the data into the concordance
wordcounts.process(data);

// This file finished
Expand All @@ -40,41 +69,43 @@ function processFile(err, data) {
}
}

var express = require('express');
var app = express();
var server = app.listen(process.env.PORT || 3000, listen);

function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
};

app.use(express.static('public'));

// Route for sending all the concordance data
app.get('/all', showAll);

// Callback
function showAll(req, res) {
// Send the entire concordance
// express automatically renders objects as JSON
res.send(wordcounts);
}

// Now a route for data about one word
app.get('/search/:word', showOne);

// Callback for the above route
function showOne(req, res) {

// Get the word
var word = req.params['word'];

// Put together a reply
var reply = { };

// Get count from concordance
var count = wordcounts.getCount(word);

// If it's not part of concordance send back a message
if (count === undefined) {
reply.status = 'word not found';
// Otherwise send back the word and count
} else {
reply.status = 'success';
reply.word = word;
reply.count = count;
}

// Output the JSON
res.send(reply);
}

Expand Down
4 changes: 3 additions & 1 deletion week9/06_scraping_proxy_express/package.json
Expand Up @@ -4,11 +4,13 @@
"description": "Express URL proxy",
"main": "server.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.13.3"
"express": "^4.13.3",
"request": "^2.65.0"
}
}
76 changes: 31 additions & 45 deletions week9/06_scraping_proxy_express/server.js
@@ -1,66 +1,52 @@
// A2Z F15
// Daniel Shiffman
// Programming from A to Z, Fall 2014
// https://github.com/shiffman/Programming-from-A-to-Z-F14

// Thanks Sam Lavigne and Shawn Van Every
// https://github.com/antiboredom/servi.js/wiki


var http = require('http');
// https://github.com/shiffman/A2Z-F15

// Using express: http://expressjs.com/
var express = require('express');
// Create the app
var app = express();

// Set up the server
// process.env.PORT is related to deploying on heroku
var server = app.listen(process.env.PORT || 3000, listen);

// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
console.log("scraping proxy starting");
};
}

// This is basically just like 'python -m SimpleHTTPServer'
// We are just serving up a directory of files
// This is for hosting files
// Anything in the public directory will be served
// This is just like python -m SimpleHTTPServer
// We could also add routes, but aren't doing so here
app.use(express.static('public'));

// A router to load a URL
app.get('/load', loadURL);

// This is a module for HTTP Requests
var request = require('request');

// Callback
function loadURL(req, res) {
// Here's the string we are seraching for
// Get the URL from the user
var url = req.query.url;
var regex = /https?:\/\/([^\s\/]+)(.*)/;
var matches = url.match(regex);

var ahost = matches[1];
var apath = '/';
if (matches[2]) {
apath = matches[2];

// Execute the HTTP Request
request(url, loaded);

// Callback for when the request is complete
function loaded(error, response, body) {
// Check for errors
if (!error && response.statusCode == 200) {
// The raw HTML is in body
res.send(body);
} else {
res.send('error');
}
}

var options = {
host: ahost,
path: apath
};

// This makes the request
http.request(options, loaded).end();

function loaded(response) {
var str = '';

// Some more data has come in
response.on('data', function (chunk) {
str += chunk;
});

// The request is finished
response.on('end', function () {
//console.log('Got soemtjing: ' + str);
res.send(str);
});
}


}

17 changes: 6 additions & 11 deletions week9/07_twitter_api_oauth/public/index.html
Expand Up @@ -17,30 +17,25 @@
border-style: solid;
border-width: 1px;
padding: 20px;
margin-top: 24px;
margin-bottom: 4px;
}
.apiquery {
margin: 0 0 20 0;
}
</style>
</head>
<body>
<h1>Twitter</h1>

<p>This page pulls data from Twitter. Authentication is happening in node on the server.</p>
<p>This page pulls data from Twitter. Authentication is happening in node on the server. </p>

<p class = 'apiquery'>
<div class ='info'>Search for tweets:</div>
<p>
Search for tweets:
<input id='searchInput' value = 'JavaScript'></input>
<button id = 'searchButton'>Search!</button>
</p>

<p class = 'apiquery'>
<div class ='info'>Tweet this:</div>
<p>
Tweet this:
<input id='postInput' value = 'A test tweet.' size='50'></input>
<button id = 'postButton'>Tweet!</button>
<div id='numchars' style='display:inline'></div>
<span id='numchars'></span>
</p>
</body>
</html>

0 comments on commit f0a1a69

Please sign in to comment.