Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
69 changed files
with
1,095 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); | |||
|
|||
}); | |||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 not shown.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+25.5 KB
episode_012/static/uploads/photos/apple-macbook-pro-17-inch-unibody-4g8-460.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,8 @@ | |||
!!! 5 | |||
html | |||
head | |||
link(rel='stylesheet', href='/stylesheets/styles.css') | |||
title My template | |||
body | |||
#main | |||
#container!= body |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
p | |||
label(for='photo') Photo: | |||
p | |||
input(type='file', name='photo') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,5 @@ | |||
h2= product.name | |||
p= product.description | |||
p Price: #{product.price} | |||
p | |||
a(href='/products/'+product.id) Details |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,7 @@ | |||
h1 Photos: | |||
p | |||
a(href='/photos/new') Create | |||
ul | |||
- each photo in photos | |||
li | |||
img(src=photo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
h1 New Photo | |||
form(action='/photos', method='POST', enctype='multipart/form-data')!= partial('photo_form') | |||
p | |||
input(type='submit') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,4 @@ | |||
h1 Products: | |||
#products!= partial('product', {collection: products}) | |||
p | |||
a(href='/products/new') New |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
h1 New product | |||
form(action='/products', method='POST')!= partial('product_form', {locals: {product: product}}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,2 @@ | |||
h2 Hello | |||
p World! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |
Oops, something went wrong.