Skip to content
Myr

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

11 files+0 added−444 removed

deleted:examples/blog/models/post.js-67 removed
deleted:examples/blog/public/style.css-56 removed
deleted:examples/blog/routes/post.js-97 removed
12/**3 * Module dependencies.4 */56var basicAuth = require('../../..').basicAuth7 , Post = require('../models/post');89module.exports = function(app){1011 /**12 * Apply basic auth to all post related routes13 */1415 app.all('/post(/*)?', basicAuth(function(user, pass){16 return 'admin' == user && 'express' == pass;17 }));1819 /**20 * Map :post to the database, loading21 * every time :post is present.22 */2324 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 });3233 /**34 * Add a post.35 */3637 app.get('/post/add', function(req, res){38 res.render('post/form', { post: {}});39 });4041 /**42 * Save a post.43 */4445 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 }5455 post.save(function(err){56 req.session.message = 'Successfully created the post.';57 res.redirect('/post/' + post.id);58 }); 59 });60 });6162 /**63 * Display the post.64 */6566 app.get('/post/:post', function(req, res){67 res.render('post', { post: req.post });68 });6970 /**71 * Display the post edit form.72 */7374 app.get('/post/:post/edit', function(req, res){75 res.render('post/form', { post: req.post });76 });7778 /**79 * Update post. Typically a data layer would handle this stuff.80 */8182 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 }8990 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
deleted:examples/blog/views/post/form.jade-22 removed
1extends ../layout23block content4 if post.title5 h1 Editing #{post.title}6 else7 h1 New Post89 include ../messages1011 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
1extends ../layout23block 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.body
deleted:examples/blog/views/index.jade-22 removed
1extends layout23block content4 h1 Blog56 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!!! 52html3 head4 title Blog5 link(rel='stylesheet', href='/style.css')6 body7 #container8 block content
deleted:examples/blog/views/messages.jade-6 removed
12if error3 p.error= error45if message6 p.message= message
deleted:examples/blog/index.js-50 removed
12/**3 * Module dependencies.4 */56var express = require('../../')7 , app = module.exports = express();89// config1011app.set('views', __dirname + '/views');12app.set('view engine', 'jade');1314// middleware1516app.configure('development',function(){17 app.use(express.logger('dev'));18})1920app.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});2930// Locals3132app.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});4142// Routes4344require('./routes/site')(app);45require('./routes/post')(app);4647if (!module.parent) {48 app.listen(3000);49 console.log('Express started on port 3000');50}
deleted:test/acceptance/blog.js-79 removed
12var app = require('../../examples/blog')3 , request = require('../support/http');45var authorization = 'Basic ' + Buffer('admin:express').toString('base64');67function 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}1415describe('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 })2324 describe('GET /post/add', function(){25 it('should require auth', function(done){26 request(app)27 .get('/post/add')28 .expect(401, done)29 })3031 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 })3839 describe('POST /post', function(){40 it('should require auth', function(done){41 request(app)42 .post('/post')43 .expect(401, done)44 })4546 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 })5253 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 })6162 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 })7172 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})