Skip to content

Commit

Permalink
Refactoring repo to have directory-to-chapter mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanRBrown committed Jun 14, 2015
1 parent d2a8aa1 commit b330a3f
Show file tree
Hide file tree
Showing 751 changed files with 214,105 additions and 81 deletions.
48 changes: 48 additions & 0 deletions ch03/meadowlark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var express = require('express');

var app = express();

// set up handlebars view engine
var handlebars = require('express3-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3000);

app.use(express.static(__dirname + '/public'));

var fortuneCookies = [
"Conquer your fears or they will conquer you.",
"Rivers need springs.",
"Do not fear what you don't know.",
"You will have a pleasant surprise.",
"Whenever possible, keep it simple.",
];

app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req,res){
var randomFortune =
fortuneCookies[Math.floor(Math.random() * fortuneCookies.length)];
res.render('about', { fortune: randomFortune });
});

// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});

// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});

app.listen(app.get('port'), function(){
console.log( 'Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.' );
});
23 changes: 23 additions & 0 deletions ch03/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "meadowlarktravel",
"version": "0.0.0",
"description": "Sample website for Web Development with Node and Express (O'Reilly).",
"main": "meadowlark.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/EthanRBrown/web-development-with-node-and-express.git"
},
"author": "Ethan Brown <e@zepln.com>",
"license": "GPL, MIT",
"bugs": {
"url": "https://github.com/EthanRBrown/web-development-with-node-and-express/issues"
},
"homepage": "https://github.com/EthanRBrown/web-development-with-node-and-express",
"dependencies": {
"express": "~4.4.2",
"express3-handlebars": "~0.5.0"
}
}
Binary file added ch03/public/img/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions ch03/views/about.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>About Meadowlark Travel</h1>

<p>Your fortune for the day:</p>
<blockquote>{{fortune}}</blockquote>
1 change: 1 addition & 0 deletions ch03/views/home.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome to Meadowlark Travel</h1>
10 changes: 10 additions & 0 deletions ch03/views/layouts/main.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>Meadowlark Travel</title>
</head>
<body>
<header><img src="/img/logo.png" alt="Meadowlark Travel Logo"></header>
{{{body}}}
</body>
</html>
File renamed without changes.
39 changes: 39 additions & 0 deletions ch04/meadowlark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var express = require('express');
var fortune = require('./lib/fortune.js');

var app = express();

// set up handlebars view engine
var handlebars = require('express3-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3000);

app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req,res){
res.render('about', { fortune: fortune.getFortune() } );
});

// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});

// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});

app.listen(app.get('port'), function(){
console.log( 'Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.' );
});
23 changes: 23 additions & 0 deletions ch04/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "meadowlarktravel",
"version": "0.0.0",
"description": "Sample website for Web Development with Node and Express (O'Reilly).",
"main": "meadowlark.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/EthanRBrown/web-development-with-node-and-express.git"
},
"author": "Ethan Brown <e@zepln.com>",
"license": "GPL, MIT",
"bugs": {
"url": "https://github.com/EthanRBrown/web-development-with-node-and-express/issues"
},
"homepage": "https://github.com/EthanRBrown/web-development-with-node-and-express",
"dependencies": {
"express": "~4.4.2",
"express3-handlebars": "~0.5.0"
}
}
Binary file added ch04/public/img/logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ch04/public/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ch04/views/404.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>404 - Not Found</h1>
1 change: 1 addition & 0 deletions ch04/views/500.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>500 - Server Error</h1>
4 changes: 4 additions & 0 deletions ch04/views/about.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>About Meadowlark Travel</h1>

<p>Your fortune for the day:</p>
<blockquote>{{fortune}}</blockquote>
1 change: 1 addition & 0 deletions ch04/views/home.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome to Meadowlark Travel</h1>
10 changes: 10 additions & 0 deletions ch04/views/layouts/main.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>Meadowlark Travel</title>
</head>
<body>
<header><img src="/img/logo.png" alt="Meadowlark Travel Logo"></header>
{{{body}}}
</body>
</html>
28 changes: 28 additions & 0 deletions ch05/Gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function(grunt){

// load plugins
[
'grunt-cafe-mocha',
'grunt-contrib-jshint',
'grunt-exec',
].forEach(function(task){
grunt.loadNpmTasks(task);
});

// configure plugins
grunt.initConfig({
cafemocha: {
all: { src: 'qa/tests-*.js', options: { ui: 'tdd' }, }
},
jshint: {
app: ['meadowlark.js', 'public/js/**/*.js', 'lib/**/*.js'],
qa: ['Gruntfile.js', 'public/qa/**/*.js', 'qa/**/*.js'],
},
exec: {
linkchecker: { cmd: 'linkchecker http://localhost:3000' }
},
});

// register tasks
grunt.registerTask('default', ['cafemocha','jshint','exec']);
};
12 changes: 12 additions & 0 deletions ch05/lib/fortune.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var fortuneCookies = [
"Conquer your fears or they will conquer you.",
"Rivers need springs.",
"Do not fear what you don't know.",
"You will have a pleasant surprise.",
"Whenever possible, keep it simple.",
];

exports.getFortune = function() {
var idx = Math.floor(Math.random() * fortuneCookies.length);
return fortuneCookies[idx];
};
58 changes: 58 additions & 0 deletions ch05/meadowlark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var express = require('express');
var fortune = require('./lib/fortune.js');

var app = express();

// set up handlebars view engine
var handlebars = require('express3-handlebars')
.create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3000);

app.use(express.static(__dirname + '/public'));

// set 'showTests' context property if the querystring contains test=1
app.use(function(req, res, next){
res.locals.showTests = app.get('env') !== 'production' &&
req.query.test === '1';
next();
});

app.get('/', function(req, res) {
res.render('home');
});
app.get('/about', function(req,res){
res.render('about', {
fortune: fortune.getFortune(),
pageTestScript: '/qa/tests-about.js'
} );
});
app.get('/tours/hood-river', function(req, res){
res.render('tours/hood-river');
});
app.get('/tours/oregon-coast', function(req, res){
res.render('tours/oregon-coast');
});
app.get('/tours/request-group-rate', function(req, res){
res.render('tours/request-group-rate');
});

// 404 catch-all handler (middleware)
app.use(function(req, res, next){
res.status(404);
res.render('404');
});

// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('500');
});

app.listen(app.get('port'), function(){
console.log( 'Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.' );
});
32 changes: 32 additions & 0 deletions ch05/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "meadowlarktravel",
"version": "0.0.0",
"description": "Sample website for Web Development with Node and Express (O'Reilly).",
"main": "meadowlark.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/EthanRBrown/web-development-with-node-and-express.git"
},
"author": "Ethan Brown <e@zepln.com>",
"license": "GPL, MIT",
"bugs": {
"url": "https://github.com/EthanRBrown/web-development-with-node-and-express/issues"
},
"homepage": "https://github.com/EthanRBrown/web-development-with-node-and-express",
"dependencies": {
"express": "~4.4.2",
"express3-handlebars": "~0.5.0",
"chai": "~1.9.1"
},
"devDependencies": {
"grunt": "^0.4.5",
"grunt-cafe-mocha": "^0.1.12",
"grunt-contrib-jshint": "^0.10.0",
"grunt-exec": "^0.4.5",
"mocha": "~1.20.1",
"zombie": "~2.0.0-alpha31"
}
}
42 changes: 42 additions & 0 deletions ch05/qa/tests-crosspage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
var Browser = require('zombie'),
assert = require('chai').assert;

var browser;

suite('Cross-Page Tests', function(){

setup(function(){
browser = new Browser();
});

test('requesting a group rate quote from the hood river tour page should ' +
'populate the hidden referrer field correctly', function(done){
var referrer = 'http://localhost:3000/tours/hood-river';
browser.visit(referrer, function(){
browser.clickLink('.requestGroupRate', function(){
assert(browser.field('referrer').value === referrer);
done();
});
});
});

test('requesting a group rate from the oregon coast tour page should ' +
'populate the hidden referrer field correctly', function(done){
var referrer = 'http://localhost:3000/tours/oregon-coast';
browser.visit(referrer, function(){
browser.clickLink('.requestGroupRate', function(){
assert(browser.field('referrer').value === referrer);
done();
});
});
});

test('visiting the "request group rate" page dirctly should result ' +
'in an empty value for the referrer field', function(done){
browser.visit('http://localhost:3000/tours/request-group-rate', function(){
assert(browser.field('referrer').value === '');
done();
});
});

});
File renamed without changes.
1 change: 1 addition & 0 deletions ch05/views/404.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>404 - Not Found</h1>
1 change: 1 addition & 0 deletions ch05/views/500.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>500 - Server Error</h1>
File renamed without changes.
1 change: 1 addition & 0 deletions ch05/views/home.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>Welcome to Meadowlark Travel</h1>
28 changes: 28 additions & 0 deletions ch05/views/layouts/main.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html>
<head>
<title>Meadowlark Travel</title>
{{#if showTests}}
<link rel="stylesheet" href="/vendor/mocha.css">
{{/if}}
<script src="//code.jquery.com/jquery-2.0.2.min.js"></script>
</head>
<body>
<header><img src="/img/logo.png" alt="Meadowlark Travel Logo"></header>
{{{body}}}
{{#if showTests}}
<div id="mocha"></div>
<script src="/vendor/mocha.js"></script>
<script src="/vendor/chai.js"></script>
<script>
mocha.ui('tdd');
var assert = chai.assert;
</script>
<script src="/qa/tests-global.js"></script>
{{#if pageTestScript}}
<script src="{{pageTestScript}}"></script>
{{/if}}
<script>mocha.run();</script>
{{/if}}
</body>
</html>
2 changes: 2 additions & 0 deletions ch05/views/tours/hood-river.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Hood River Tour</h1>
<a class="requestGroupRate" href="/tours/request-group-rate">Request group rate.</a>
2 changes: 2 additions & 0 deletions ch05/views/tours/oregon-coast.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Oregon Coast Tour</h1>
<a class="requestGroupRate" href="/tours/request-group-rate">Request group rate.</a>
Loading

0 comments on commit b330a3f

Please sign in to comment.