Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: okamuuu/Practice-NodeTut
base: e02d009542
...
head fork: okamuuu/Practice-NodeTut
compare: d1dd39a9d4
  • 2 commits
  • 27 files changed
  • 0 commit comments
  • 1 contributor
Showing with 492 additions and 0 deletions.
  1. +87 −0 episode11/app.js
  2. +51 −0 episode11/products.js
  3. +11 −0 episode11/static/stylesheets/style.css
  4. +9 −0 episode11/views/layout.jade
  5. +9 −0 episode11/views/partials/product.jade
  6. +15 −0 episode11/views/partials/product_form.jade
  7. +4 −0 episode11/views/products/edit.jade
  8. +4 −0 episode11/views/products/index.jade
  9. +3 −0  episode11/views/products/new.jade
  10. +7 −0 episode11/views/products/show.jade
  11. +2 −0  episode11/views/root.jade
  12. +136 −0 episode12/app.js
  13. +16 −0 episode12/photos.js
  14. +51 −0 episode12/products.js
  15. +11 −0 episode12/static/stylesheets/style.css
  16. BIN  episode12/static/uploads/photos/imgres.jpeg
  17. +9 −0 episode12/views/layout.jade
  18. +4 −0 episode12/views/partials/photo_form.jade
  19. +9 −0 episode12/views/partials/product.jade
  20. +19 −0 episode12/views/partials/product_form.jade
  21. +7 −0 episode12/views/photos/index.jade
  22. +4 −0 episode12/views/photos/new.jade
  23. +4 −0 episode12/views/products/edit.jade
  24. +4 −0 episode12/views/products/index.jade
  25. +3 −0  episode12/views/products/new.jade
  26. +11 −0 episode12/views/products/show.jade
  27. +2 −0  episode12/views/root.jade
View
87 episode11/app.js
@@ -0,0 +1,87 @@
+var express = require('express');
+
+var app = express.createServer();
+
+app.configure(function () {
+ app.use(express.logger());
+ // renamed bodyDecoder to podyParser from express v2.3.2
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ // staticProvider has gone. we can use static instead of it now.
+ app.use(express.static(__dirname + '/static'));
+});
+
+app.configure('development', function () {
+ app.use(express.errorHandler({
+ dumpExceptions: true,
+ showStack: true
+ }));
+});
+
+app.configure('production', function () {
+ app.use(express.errorHandler({}));
+});
+
+app.set('views', __dirname + '/views');
+app.set('view engine', 'jade');
+app.set('view options', {layout: true});
+
+app.get('/', function (req, res) {
+ res.render('root');
+});
+
+var products = require('./products');
+
+app.get('/products', function (req, res) {
+ res.render('products/index', {locals: {
+ products: products.all
+ }});
+});
+
+app.get('/products/new', function(req, res) {
+ res.render('products/new', {locals: {
+ product: req.body && req.body.product || products.new()
+ }});
+});
+
+app.post('/products', function(req, res) {
+ var id = products.insert(req.body.product);
+ res.redirect('/products/' + id);
+});
+
+
+
+app.get('/products/:id', function(req, res) {
+ var product = products.find(req.params.id);
+ if(product != null) {
+ res.render('products/show', {locals: {
+ product: product
+ }});
+ } else {
+ res.send('404 - Product not found!')
+ }
+});
+
+app.get('/products/:id/edit', function (req, res) {
+ var product = products.find(req.params.id);
+
+ res.render('products/edit', {locals: {
+ product: product
+ }});
+});
+
+app.put('/products/:id', function(req, res) {
+ var id = req.params.id;
+ // req.body.product comes from bodyParser
+ console.log(req.body);
+ console.log(req.body.product);
+ products.set(id, req.body.product);
+ res.redirect('/products/' + id);
+});
+
+
+
+
+
+
+app.listen(4000);
View
51 episode11/products.js
@@ -0,0 +1,51 @@
+products = [
+{
+ id: 1,
+ name: 'Mac Book Pro',
+ description: 'Apple 13 inch Mac Book Pro Notebook',
+ price: 1000
+},
+{
+ id: 2,
+ name: 'iPad',
+ description: 'Apple 64GB 3G iPad',
+ price: 899
+}
+];
+
+module.exports.all = products;
+
+module.exports.find = function(id) {
+ id = parseInt(id, 10);
+ var found = null;
+ productloop: for(product_index in products) {
+ var product = products[product_index];
+ if(product.id == id) {
+ found = product;
+ break productloop;
+ }
+ };
+ return found;
+}
+
+module.exports.set = function(id, product) {
+ id = parseInt(id, 10);
+ product.id = id;
+ products[id -1] = product;
+}
+
+module.exports.new = function() {
+ return {
+ name: '',
+ description: '',
+ price: 0
+ };
+}
+
+module.exports.insert = function(product) {
+ var id = products.length + 1;
+ product.id = id;
+ products[id-1] = product;
+ return id;
+}
+
View
11 episode11/static/stylesheets/style.css
@@ -0,0 +1,11 @@
+body {
+ font-family: Helvetica, Arial, Sans-Serif;
+ font-size: 16px;
+}
+
+#main {
+ width: 960px;
+ margin: 0 auto;
+ border: 1px solid #ddd;
+ padding: 1em;
+}
View
9 episode11/views/layout.jade
@@ -0,0 +1,9 @@
+!!! 5
+html
+ head
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ title My template
+ body
+ #main
+ h1 Content goes here
+ #container!= body
View
9 episode11/views/partials/product.jade
@@ -0,0 +1,9 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+p
+ a(href='/products/' + product.id) Details
+- if(product.photo)
+ p
+ img(src=product.photo)
+
View
15 episode11/views/partials/product_form.jade
@@ -0,0 +1,15 @@
+p
+ label(for='product_name') Name:
+p
+ input(type='text', id='product_name', name='product[name]', value=product.name)
+p
+ label(for='product_description') Description:
+p
+ textarea(id='product_description', name='product[description]')= product.description
+p
+ label(for='product_price') Price:
+p
+ input(type='text', id='product_price', name='product[price]', value=product.price)
+p
+ input(type='submit')
+
View
4 episode11/views/products/edit.jade
@@ -0,0 +1,4 @@
+h1 Editing #{product.name}
+form(action='/products/' + product.id, method='POST')
+ input(type='hidden', name='_method', value='PUT')
+ div!= partial('../partials/product_form', {locals: {product: product}})
View
4 episode11/views/products/index.jade
@@ -0,0 +1,4 @@
+h1 Products:
+#products!= partial('../partials/product', {collection: products})
+p
+ a(href='/products/new') New
View
3  episode11/views/products/new.jade
@@ -0,0 +1,3 @@
+h1 Editing #{product.name}
+form(action='/products', method='POST')
+ div!= partial('../partials/product_form', {locals: {product: product}})
View
7 episode11/views/products/show.jade
@@ -0,0 +1,7 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+p
+ a(href='/products') Back
+p
+ a(href='/products/' + product.id + '/edit') Edit
View
2  episode11/views/root.jade
@@ -0,0 +1,2 @@
+h2 hello
+p World!
View
136 episode12/app.js
@@ -0,0 +1,136 @@
+var express = require('express'),
+ form = require('connect-form'),
+ fs = require('fs'),
+ util = require('util');
+
+var app = express.createServer(
+ form({keepExtensions: true})
+);
+
+app.configure(function () {
+ app.use(express.logger());
+ // renamed bodyDecoder to podyParser from express v2.3.2
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ // staticProvider has gone. we can use static instead of it now.
+ app.use(express.static(__dirname + '/static'));
+});
+
+app.configure('development', function () {
+ app.use(express.errorHandler({
+ dumpExceptions: true,
+ showStack: true
+ }));
+});
+
+app.configure('production', function () {
+ app.use(express.errorHandler({}));
+});
+
+app.set('views', __dirname + '/views');
+app.set('view engine', 'jade');
+app.set('view options', {layout: true});
+
+app.get('/', function (req, res) {
+ res.render('root');
+});
+
+var products = require('./products');
+var photos = require('./photos');
+
+app.get('/products', function (req, res) {
+ res.render('products/index', {locals: {
+ products: products.all
+ }});
+});
+
+app.get('/products/new', function(req, res) {
+ res.render('products/new', {locals: {
+ product: req.body && req.body.product || products.new()
+ }});
+});
+
+app.post('/products', function(req, res) {
+ var id = products.insert(req.body.product);
+ res.redirect('/products/' + id);
+});
+
+app.get('/products/:id', function(req, res) {
+ var product = products.find(req.params.id);
+ if(product !== null) {
+ console.log(product);
+ res.render('products/show', {locals: {
+ product: product
+ }});
+ } else {
+ res.send('404 - Product not found!');
+ }
+});
+
+app.get('/products/:id/edit', function (req, res) {
+ var product = products.find(req.params.id);
+
+ if(product !== null) {
+ photos.list(function(err, photo_list) {
+ if(err) {
+ throw err;
+ } else {
+ res.render('products/edit', {locals: {
+ product: product,
+ photos: photo_list
+ }});
+ }
+ });
+ }
+ else {
+ res.send('404 - Product not found!');
+ }
+});
+
+app.put('/products/:id', function(req, res) {
+ var id = req.params.id;
+ // req.body.product comes from bodyParser
+ console.log(req.body);
+ console.log(req.body.product);
+ products.set(id, req.body.product);
+ res.redirect('/products/' + id);
+});
+
+/* Photos */
+app.get('/photos', function(req, res) {
+ photos.list(function(err,photo_list) {
+ res.render('photos/index', {locals : {
+ photos: photo_list
+ }});
+ });
+});
+
+app.get('/photos/new', function(req, res) {
+ res.render('photos/new');
+});
+
+app.post('/photos', function(req, res) {
+ console.log(req.files);
+
+ var ins = fs.createReadStream(req.files.photo.path);
+ var ous = fs.createWriteStream(__dirname + '/static/uploads/photos/' + req.files.photo.filename);
+
+ util.pump(ins, ous, function(err) {
+ if(err) {
+ console.log(err);
+ }
+ else {
+ res.redirect('/photos');
+ }
+ });
+});
+
+/* 404 redirect here */
+app.get('*', function(req, res) {
+ res.send('404 - no such page');
+});
+
+app.listen(4000);
+
+console.log('Server listening on port 4000');
+
View
16 episode12/photos.js
@@ -0,0 +1,16 @@
+var fs = require('fs'),
+ src_path = __dirname + '/static/uploads/photos/';
+
+module.exports.list = function(callback) {
+ fs.readdir(src_path, function(err, files) {
+ var ret_files = [];
+ if (files) {
+ files.forEach(function(file){
+ ret_files.push('/uploads/photos/' + file);
+ });
+ }
+
+ console.log(ret_files);
+ callback(err, ret_files);
+ });
+}
View
51 episode12/products.js
@@ -0,0 +1,51 @@
+products = [
+{
+ id: 1,
+ name: 'Mac Book Pro',
+ description: 'Apple 13 inch Mac Book Pro Notebook',
+ price: 1000
+},
+{
+ id: 2,
+ name: 'iPad',
+ description: 'Apple 64GB 3G iPad',
+ price: 899
+}
+];
+
+module.exports.all = products;
+
+module.exports.find = function(id) {
+ id = parseInt(id, 10);
+ var found = null;
+ productloop: for(product_index in products) {
+ var product = products[product_index];
+ if(product.id == id) {
+ found = product;
+ break productloop;
+ }
+ };
+ return found;
+}
+
+module.exports.set = function(id, product) {
+ id = parseInt(id, 10);
+ product.id = id;
+ products[id -1] = product;
+}
+
+module.exports.new = function() {
+ return {
+ name: '',
+ description: '',
+ price: 0
+ };
+}
+
+module.exports.insert = function(product) {
+ var id = products.length + 1;
+ product.id = id;
+ products[id-1] = product;
+ return id;
+}
+
View
11 episode12/static/stylesheets/style.css
@@ -0,0 +1,11 @@
+body {
+ font-family: Helvetica, Arial, Sans-Serif;
+ font-size: 16px;
+}
+
+#main {
+ width: 960px;
+ margin: 0 auto;
+ border: 1px solid #ddd;
+ padding: 1em;
+}
View
BIN  episode12/static/uploads/photos/imgres.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
9 episode12/views/layout.jade
@@ -0,0 +1,9 @@
+!!! 5
+html
+ head
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ title My template
+ body
+ #main
+ h1 Content goes here
+ #container!= body
View
4 episode12/views/partials/photo_form.jade
@@ -0,0 +1,4 @@
+p
+ label(for='photo') Photo:
+p
+ input(type='file', name='photo')
View
9 episode12/views/partials/product.jade
@@ -0,0 +1,9 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+p
+ a(href='/products/' + product.id) Details
+- if(product.photo)
+ p
+ img(src=product.photo)
+
View
19 episode12/views/partials/product_form.jade
@@ -0,0 +1,19 @@
+p
+ label(for='product_name') Name:
+p
+ input(type='text', id='product_name', name='product[name]', value=product.name)
+p
+ label(for='product_description') Description:
+p
+ textarea(id='product_description', name='product[description]')= product.description
+p
+ label(for='product_price') Price:
+p
+ input(type='text', id='product_price', name='product[price]', value=product.price)
+p
+ select(id='product_photo', name='product[photo]')
+ - each photo in photos
+ option(value=photo, selected=(product.photo == photo))= photo
+p
+ input(type='submit')
+
View
7 episode12/views/photos/index.jade
@@ -0,0 +1,7 @@
+h1 Photos:
+p
+ a(href='/photos/new') Create
+ul
+ - each photo in photos
+ li
+ img(src=photo)
View
4 episode12/views/photos/new.jade
@@ -0,0 +1,4 @@
+h1 New Photo
+form(action='/photos', method='POST', enctype='multipart/form-data')!= partial('../partials/photo_form')
+ p
+ input(type='submit')
View
4 episode12/views/products/edit.jade
@@ -0,0 +1,4 @@
+h1 Editing #{product.name}
+form(action='/products/' + product.id, method='POST')
+ input(type='hidden', name='_method', value='PUT')
+ div!= partial('../partials/product_form', {locals: {product: product}})
View
4 episode12/views/products/index.jade
@@ -0,0 +1,4 @@
+h1 Products:
+#products!= partial('../partials/product', {collection: products})
+p
+ a(href='/products/new') New
View
3  episode12/views/products/new.jade
@@ -0,0 +1,3 @@
+h1 Editing #{product.name}
+form(action='/products', method='POST')
+ div!= partial('../partials/product_form', {locals: {product: product}})
View
11 episode12/views/products/show.jade
@@ -0,0 +1,11 @@
+h2= product.name
+p= product.description
+p Price: #{product.price}
+- if(product.photo)
+ p
+ img(src=product.photo)
+
+p
+ a(href='/products') Back
+p
+ a(href='/products/' + product.id + '/edit') Edit
View
2  episode12/views/root.jade
@@ -0,0 +1,2 @@
+h2 hello
+p World!

No commit comments for this range

Something went wrong with that request. Please try again.