Removed blog example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
need a better one
authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 833a487 · Browse files · 11 files·+0 added−444 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+0 added−444 removed
- tests:+0 added−79 removed
- other:+0 added−365 removed
Provenance
- Commit
9ed1f2a- Tree
f1e1951- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
833a487
11 files changed+0 added−444 removed
deleted:examples/blog/models/post.js-67 removed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
// Fake data store
var ids = 0 , db = {};
var Post = exports = module.exports = function Post(title, body) { this.id = ++ids; this.title = title; this.body = body; this.createdAt = new Date;};
Post.prototype.save = function(fn){ db[this.id] = this; fn();};
Post.prototype.validate = function(fn){ if (!this.title) return fn(new Error('title required')); if (!this.body) return fn(new Error('body required')); if (this.body.length < 10) { return fn(new Error( 'body should be at least 10 characters long, was only ' + this.body.length)); } 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);};
exports.count = function(fn){ fn(null, Object.keys(db).length);};
exports.all = function(fn){ var arr = Object.keys(db).reduce(function(arr, id){ arr.push(db[id]); return arr; }, []); fn(null, arr);};
exports.get = function(id, fn){ fn(null, db[id]);};
exports.destroy = function(id, fn) { if (db[id]) { delete db[id]; fn(); } else { fn(new Error('post ' + id + ' does not exist')); }};No newline at end of filedeleted:examples/blog/public/style.css-56 removed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556body { font: 13px "Helvetica Neue", Arial, sans-serif; color: #111; padding: 60px 80px;}h1, h2 { color: #c00;}a { color: #c00; text-decoration: none;}a:hover { text-decoration: underline;}label { padding: 6px 0; display: block; text-transform: lowercase;}textarea,input { outline: none; padding: 5px; background: #f1f1f1; border: 1px solid #aaa;}textarea:focus,input:focus { background: #fff;}textarea { width: 500px; height: 200px;}a.edit { margin-left: 10px; font-size: 11px; font-weight: normal;}.date { font-size: 11px;}p.error,p.message { padding: 10px; border: 1px solid;}p.error { background: #FDEAE7; color: #E4250C;}p.message { color: #2EBBE6; background: #F7FBFD;}No newline at end of filedeleted:examples/blog/routes/post.js-97 removed
1-2-/**3- * Module dependencies.4- */5-6-var basicAuth = require('../../..').basicAuth7- , Post = require('../models/post');8-9-module.exports = function(app){10-11- /**12- * Apply basic auth to all post related routes13- */14-15- app.all('/post(/*)?', basicAuth(function(user, pass){16- return 'admin' == user && 'express' == pass;17- }));18-19- /**20- * Map :post to the database, loading21- * every time :post is present.22- */23-24- app.param('post', function(req, res, next, id){25- Post.get(id, function(err, post){26- if (err) return next(err);27- if (!post) return next(new Error('failed to load post ' + id));28- req.post = post;29- next();30- });31- });32-33- /**34- * Add a post.35- */36-37- app.get('/post/add', function(req, res){38- res.render('post/form', { post: {}});39- });40-41- /**42- * Save a post.43- */44-45- app.post('/post', function(req, res){46- var data = req.body.post || {}47- , post = new Post(data.title, data.body);48- 49- post.validate(function(err){50- if (err) {51- req.session.error = err.message;52- return res.redirect('back');53- }54-55- post.save(function(err){56- req.session.message = 'Successfully created the post.';57- res.redirect('/post/' + post.id);58- }); 59- });60- });61-62- /**63- * Display the post.64- */65-66- app.get('/post/:post', function(req, res){67- res.render('post', { post: req.post });68- });69-70- /**71- * Display the post edit form.72- */73-74- app.get('/post/:post/edit', function(req, res){75- res.render('post/form', { post: req.post });76- });77-78- /**79- * Update post. Typically a data layer would handle this stuff.80- */81-82- app.put('/post/:post', function(req, res, next){83- var post = req.post;84- post.validate(function(err){85- if (err) {86- req.session.error = err.message;87- return res.redirect('back');88- }89-90- post.update(req.body.post, function(err){91- if (err) return next(err);92- req.session.message = 'Successfully updated post';93- res.redirect('back');94- });95- });96- });97-};deleted:examples/blog/routes/site.js-19 removed
12345678910111213141516171819
/** * Module dependencies. */
var Post = require('../models/post');
module.exports = function(app){ app.get('/', function(req, res){ Post.count(function(err, count){ Post.all(function(err, posts){ res.render('index', { count: count , posts: posts }); }); }); });};No newline at end of filedeleted:examples/blog/views/post/form.jade-22 removed
1-extends ../layout2-3-block content4- if post.title5- h1 Editing #{post.title}6- else7- h1 New Post8-9- include ../messages10-11- form#post(action='/post' + (post.title ? '/' + post.id : ''), method='post')12- if post.title13- input(type='hidden', name='_method', value='put')14- p15- label(for='post[title]') Title:16- input(type='text', name='post[title]', value=post.title)17- p18- label(for='post[body]') Body:19- textarea(name='post[body]')= post.body || ''20- p21- input(type='submit', value=post.title ? 'Update' : 'Create')22- deleted:examples/blog/views/post/index.jade-18 removed
1-extends ../layout2-3-block content4- .post5- // title6- h27- = post.title8- a.edit(href='/post/' + post.id + '/edit') Edit9- 10- include ../messages11- 12- // dates13- p.date.created Created at #{post.createdAt}14- if post.updatedAt15- p.date.updated Updated at #{post.updatedAt}16- 17- // body18- pre.body= post.bodydeleted:examples/blog/views/index.jade-22 removed
1-extends layout2-3-block content4- h1 Blog5-6- if count7- p Display all #{count} post(s)8- #posts9- each post in posts10- include post/index11- else12- p 13- | It looks like you have no posts!14- p15- | Click16- a(href='/post/add') here 17- | to create a post. Login18- | as19- em "admin" 20- | and21- em "express"22- | .deleted:examples/blog/views/layout.jade-8 removed
1-!!! 52-html3- head4- title Blog5- link(rel='stylesheet', href='/style.css')6- body7- #container8- block contentdeleted:examples/blog/views/messages.jade-6 removed
1-2-if error3- p.error= error4-5-if message6- p.message= messagedeleted:examples/blog/index.js-50 removed
1-2-/**3- * Module dependencies.4- */5-6-var express = require('../../')7- , app = module.exports = express();8-9-// config10-11-app.set('views', __dirname + '/views');12-app.set('view engine', 'jade');13-14-// middleware15-16-app.configure('development',function(){17- app.use(express.logger('dev'));18-})19-20-app.configure(function(){21- app.use(express.bodyParser());22- app.use(express.methodOverride());23- app.use(express.cookieParser('keyboard cat'));24- app.use(express.session());25- app.use(app.router);26- app.use(express.static(__dirname + '/public'));27- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));28-});29-30-// Locals31-32-app.locals.use(function(req, res){33- // expose "error" and "message" to all34- // views that are rendered.35- res.locals.error = req.session.error || '';36- res.locals.message = req.session.message || '';37- // remove them so they're not displayed on subsequent renders38- delete req.session.error;39- delete req.session.message;40-});41-42-// Routes43-44-require('./routes/site')(app);45-require('./routes/post')(app);46-47-if (!module.parent) {48- app.listen(3000);49- console.log('Express started on port 3000');50-}deleted:test/acceptance/blog.js-79 removed
1-2-var app = require('../../examples/blog')3- , request = require('../support/http');4-5-var authorization = 'Basic ' + Buffer('admin:express').toString('base64');6-7-function redirects(to,fn){8- return function(res){9- res.statusCode.should.equal(302)10- res.headers.should.have.property('location').match(to);11- fn()12- }13-}14-15-describe('blog', function(){16- describe('GET /', function(){17- it('should have no posts', function(done){18- request(app)19- .get('/')20- .expect(/you have no posts/, done)21- })22- })23-24- describe('GET /post/add', function(){25- it('should require auth', function(done){26- request(app)27- .get('/post/add')28- .expect(401, done)29- })30-31- it('should login', function(done){32- request(app)33- .get('/post/add')34- .set('Authorization', authorization)35- .expect(/<h1>New Post<\/h1>/, done)36- })37- })38-39- describe('POST /post', function(){40- it('should require auth', function(done){41- request(app)42- .post('/post')43- .expect(401, done)44- })45-46- it('should redirect to / with no title or body', function(done){47- request(app)48- .post('/post')49- .set('Authorization', authorization)50- .end(redirects(/\/$/, done))51- })52-53- it('should redirect to / with no body', function(done){54- request(app)55- .post('/post')56- .set('Authorization', authorization)57- .set('Content-Type', 'application/x-www-form-urlencoded')58- .write('post[title]=Kittens')59- .end(redirects(/\/$/, done))60- })61-62- it('should redirect to /post/:post when successful', function(done){63- request(app)64- .post('/post')65- .set('Authorization', authorization)66- .set('Content-Type', 'application/x-www-form-urlencoded')67- .write('post[title]=Kittens&post[body]=In+very+large+baskets')68- .end(redirects(/\/post\/\d+$/, done))69- })70- })71-72- describe('GET /', function(){73- it('should now list 1 post',function(done){74- request(app)75- .get('/')76- .expect(/Display all 1 post/, done)77- })78- })79-})