More example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent d7df142 · Browse files · 5 files·+57 added−19 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+57 added−19 removed
- other:+57 added−19 removed
Provenance
- Commit
1c40e43- Tree
05a41f0- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
d7df142
5 files changed+57 added−19 removed
modified:examples/blog/models/post.js+13 added-6 removed
6 unmodified lines789101112131314151516171819202122232425262728293031324 unmodified lines3738394030313233414243444 unmodified lines6 unmodified linesvar Post = exports = module.exports = function Post(title, body) { this.id = ++ids; this.title = title; this.body = body; this.createdAt = new Date;};
Post.prototype.update =Post.prototype.save = function(fn){ exports.update(this, fn); db[this.id] = this; fn();};
Post.prototype.update = function(data, fn){ this.updatedAt = new Date; for (var key in data) { if (undefined != data[key]) { this[key] = data[key]; } } this.save(fn);};
Post.prototype.destroy = function(fn){ exports.destroy(this.id, fn);};
4 unmodified linesexports.get = function(id, fn){ fn(null, db[id]);};
exports.update = function(post, fn){ fn(null, db[post.id] = db);};
exports.destroy = function(id, fn) { if (db[id]) { delete db[id]; fn();4 unmodified linesmodified:examples/blog/routes/post.js+35 added-10 removed
13 unmodified lines141516171818192021212223242526272822232425262728293031323313 unmodified lines474849505152535455515657585960616263646566676869707172737475767713 unmodified lines return 'admin' == user && 'express' == pass;}));
/** * Map :post to the database. * Map :post to the database, loading * every time :post is present. */
// app.param('post', function(req, res, next, id){// Post.get(id, function(err, post){// if (err) return next(err);// if (!post) return next(new Error('failed to find user ' + id));// req.post = post;// next();// });// });app.param('post', function(req, res, next, id){ Post.get(id, function(err, post){ if (err) return next(err); if (!post) return next(new Error('failed to post user ' + id)); req.post = post; next(); });});
/** * Add a post. */13 unmodified lines res.redirect('/post/' + post.id); });});
/** * Display the post. */
app.get('/post/:post', function(req, res){ console.log(req.post); res.render('post', { post: req.post });});
/** * Display the post edit form. */
app.get('/post/:post/edit', function(req, res){ res.render('post/form', { post: req.post });});
/** * Update post. Typically a data layer would handle this stuff. */
app.put('/post/:post', function(req, res, next){ var post = req.post; post.update(req.body.post, function(err){ if (err) return next(err); res.redirect('back'); });});No newline at end of fileNo newline at end of filemodified:examples/blog/views/post/form.jade+1 added-1 removed
2 unmodified lines3 h1 Editing #{post.title}4 - else5 h1 New Post6 7-form#post(action='/post', method='post')7+form#post(action='/post' + (post.title ? '/' + post.id : ''), method='post')8 - if (post.title)9 input(type='hidden', name='_method', value='put')10 p11 label(for='post[title]') Title:7 unmodified linesadded:examples/blog/views/post/index.jade+6 added
1+.post2+ h1= post.title3+ p.date.created Created at #{post.createdAt}4+ - if (post.updatedAt)5+ p.date.updated Updated at #{post.updatedAt}6+ pre.body= post.bodymodified:examples/blog/app.js+2 added-2 removed
16 unmodified lines17181920212122232424252627286 unmodified lines16 unmodified lines
// Middleware
app.configure(function(){ app.use(express.logger('\x1b[33m:method\x1b[0m \x1b[32m:url\x1b[0m')); app.use(express.logger('\x1b[33m:method\x1b[0m \x1b[32m:url\x1b[0m :response-time')); app.use(express.bodyDecoder()); app.use(express.methodOverride()); app.use(express.router); app.use(app.router); app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));});
// Routes6 unmodified lines