Skip to content

Commit

Permalink
added submodules as folders
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte committed Jul 24, 2012
1 parent 03dd70f commit ac85bc6
Show file tree
Hide file tree
Showing 69 changed files with 1,095 additions and 0 deletions.
124 changes: 124 additions & 0 deletions episode_012/app.js
@@ -0,0 +1,124 @@
var express = require('express');
var multipart = require('multipart');
var fs = require('fs');

var app = express.createServer();

app.configure(function() {
app.use(express.logger());
app.use(express.bodyDecoder());
app.use(express.methodOverride());
app.use(express.staticProvider(__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.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);
res.render('products/show', {locals: {
product: product
}});
});

app.get('/products/:id/edit', function(req, res) {
var product = products.find(req.params.id);
photos.list(function(err, photo_list) {
if (err) {
throw err;
}
res.render('products/edit', {locals: {
product: product,
photos: photo_list
}});

});
});

app.put('/products/:id', function(req, res) {
var id = req.params.id;
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) {
req.setEncoding('binary');

var parser = multipart.parser();

parser.headers = req.headers;
var ws;

parser.onPartBegin = function(part) {
ws = fs.createWriteStream(__dirname + '/static/uploads/photos/' + part.filename);
ws.on('error', function(err) {
throw err;
});
};

parser.onData = function(data) {
ws.write(data, 'binary');
};

parser.onPartEnd = function() {
ws.end();
parser.close();
res.redirect('/photos');
};

req.on('data', function(data) {
parser.write(data);
});

});

app.listen(4000);
14 changes: 14 additions & 0 deletions episode_012/photos.js
@@ -0,0 +1,14 @@
var fs = require('fs');

var src_path = __dirname + '/static/uploads/photos/'
module.exports.list = function(callback) {
fs.readdir(src_path, function(err, files) {
var ret_files = [];
files.forEach(function(file) {
ret_files.push('/uploads/photos/' + file);
});
console.log(ret_files);
callback(err, ret_files);

});
};
50 changes: 50 additions & 0 deletions episode_012/products.js
@@ -0,0 +1,50 @@
var 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;
}
10 changes: 10 additions & 0 deletions episode_012/static/stylesheets/styles.css
@@ -0,0 +1,10 @@
body {
font-family: Helvetica, Arial, Sans-Serif;
font-size: 16px;
}
#main {
width: 960px;
margin: 0 auto;
border: 1px solid #ddd;
padding: 1em;
}
Binary file added episode_012/static/uploads/photos/.DS_Store
Binary file not shown.
Binary file added episode_012/static/uploads/photos/28-ipad.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions episode_012/views/layout.jade
@@ -0,0 +1,8 @@
!!! 5
html
head
link(rel='stylesheet', href='/stylesheets/styles.css')
title My template
body
#main
#container!= body
4 changes: 4 additions & 0 deletions episode_012/views/partials/photo_form.jade
@@ -0,0 +1,4 @@
p
label(for='photo') Photo:
p
input(type='file', name='photo')
5 changes: 5 additions & 0 deletions episode_012/views/partials/product.jade
@@ -0,0 +1,5 @@
h2= product.name
p= product.description
p Price: #{product.price}
p
a(href='/products/'+product.id) Details
20 changes: 20 additions & 0 deletions episode_012/views/partials/product_form.jade
@@ -0,0 +1,20 @@
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_name', 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
label(for='product_photo') Photo:
p
select(id='product_photo', name='product[photo]')
- each photo in photos
option(value=photo, selected=(product.photo == photo))= photo
p
input(type='submit')
7 changes: 7 additions & 0 deletions episode_012/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)
4 changes: 4 additions & 0 deletions episode_012/views/photos/new.jade
@@ -0,0 +1,4 @@
h1 New Photo
form(action='/photos', method='POST', enctype='multipart/form-data')!= partial('photo_form')
p
input(type='submit')
4 changes: 4 additions & 0 deletions episode_012/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('product_form', {locals: {product: product, photos: photos}})
4 changes: 4 additions & 0 deletions episode_012/views/products/index.jade
@@ -0,0 +1,4 @@
h1 Products:
#products!= partial('product', {collection: products})
p
a(href='/products/new') New
2 changes: 2 additions & 0 deletions episode_012/views/products/new.jade
@@ -0,0 +1,2 @@
h1 New product
form(action='/products', method='POST')!= partial('product_form', {locals: {product: product}})
10 changes: 10 additions & 0 deletions episode_012/views/products/show.jade
@@ -0,0 +1,10 @@
h2= product.name
p= product.description
- if (product.photo)
p
img(src=product.photo)
p Price: #{product.price}
p
a(href='/products') Back
a
a(href='/products/'+product.id+'/edit') Edit
2 changes: 2 additions & 0 deletions episode_012/views/root.jade
@@ -0,0 +1,2 @@
h2 Hello
p World!
3 changes: 3 additions & 0 deletions episode_013/README
@@ -0,0 +1,3 @@
See Node Tuts episode 13 - Authentication in Express - Sessions and Route middleware

http://nodetuts.com/tutorials/13-authentication-in-express-sessions-and-route-middleware.html

0 comments on commit ac85bc6

Please sign in to comment.