Merge branch 'mounting'
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 · parents 4ce815a, a1609fa · Browse files · 10 files·+145 added−88 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
10 files+145 added−88 removed
- other:+145 added−88 removed
Provenance
- Commit
a572a71- Tree
128f43b- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
4ce815a- Parent 2
a1609fa
10 files changed+145 added−88 removed
modified:examples/blog/routes/post.js+70 added-67 removed
4 unmodified lines56789910111011121313141514151617171819201819202122222324252627232425262728293029313132333233343535363736373839394041404142434443444546474849505152535455454647484950515253545556575859576059606161626364636465656667686768696970717271727373747576757677777879807980818283848586878889818283848586878889909192939492954 unmodified lines
var basicAuth = require('../../../lib/express').basicAuth , Post = require('../models/post');
module.exports = function(app){/** * Apply basic auth to all post related routes */ /** * Apply basic auth to all post related routes */
app.all('/post(/*)?', basicAuth(function(user, pass){ return 'admin' == user && 'express' == pass;})); app.all('/post(/*)?', basicAuth(function(user, pass){ return 'admin' == user && 'express' == pass; }));
/** * Map :post to the database, loading * every time :post is present. */ /** * 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 load post ' + 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 load post ' + id)); req.post = post; next(); }); });});
/** * Add a post. */ /** * Add a post. */
app.get('/post/add', function(req, res){ res.render('post/form', { post: {}});}); app.get('/post/add', function(req, res){ res.render('post/form', { post: {}}); });
/** * Save a post. */ /** * Save a post. */
app.post('/post', function(req, res){app.post('/post', function(req, res){ var data = req.body.post , post = new Post(data.title, data.body); post.validate(function(err){ if (err) { req.flash('error', err.message); return res.redirect('back'); } post.save(function(err){ req.flash('info', 'Successfully created post _%s_', post.title); res.redirect('/post/' + post.id); }); var data = req.body.post , post = new Post(data.title, data.body);
post.validate(function(err){ if (err) { req.flash('error', err.message); return res.redirect('back'); }
post.save(function(err){ req.flash('info', 'Successfully created post _%s_', post.title); res.redirect('/post/' + post.id); }); }); });});
/** * Display the post. */ /** * Display the post. */
app.get('/post/:post', function(req, res){ res.render('post', { post: req.post });}); app.get('/post/:post', function(req, res){ res.render('post', { post: req.post }); });
/** * Display the post edit form. */ /** * Display the post edit form. */
app.get('/post/:post/edit', function(req, res){ res.render('post/form', { post: req.post });}); 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. */ /** * Update post. Typically a data layer would handle this stuff. */
app.put('/post/:post', function(req, res, next){ var post = req.post; post.validate(function(err){ if (err) { req.flash('error', err.message); return res.redirect('back'); } post.update(req.body.post, function(err){ if (err) return next(err); req.flash('info', 'Successfully updated post'); res.redirect('back'); app.put('/post/:post', function(req, res, next){ var post = req.post; post.validate(function(err){ if (err) { req.flash('error', err.message); return res.redirect('back'); } post.update(req.body.post, function(err){ if (err) return next(err); req.flash('info', 'Successfully updated post'); res.redirect('back'); }); }); });});No newline at end of file};No newline at end of filemodified:examples/blog/routes/site.js+9 added-7 removed
3 unmodified lines456788910111213910111213141516171817193 unmodified lines */
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 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 filemodified:examples/blog/views/post/form.jade+1 added-1 removed
4 unmodified lines5 h1 New Post6 7 != messages()8 9-form#post(action='/post' + (post.title ? '/' + post.id : ''), method='post')9+form#post(action=base + '/post' + (post.title ? '/' + post.id : ''), method='post')10 - if (post.title)11 input(type='hidden', name='_method', value='put')12 p13 label(for='post[title]') Title:7 unmodified linesmodified:examples/blog/views/post/index.jade+1 added-1 removed
1 .post2 // title3 h24 = post.title5- a.edit(href='/post/' + post.id + '/edit') Edit5+ a.edit(href=base + '/post/' + post.id + '/edit') Edit6 7 // flash messages8 != messages()9 7 unmodified linesmodified:examples/blog/views/index.jade+1 added-1 removed
8 unmodified lines9 p 10 | It looks like you have no posts!11 p12 | Click13- a(href='/post/add') here13+ a(href=base + '/post/add') here14 | to create a post. Login15 | as16 em "admin"17 | and2 unmodified linesmodified:examples/blog/views/layout.jade+1 added-1 removed
1 !!! 52 html3 head4 title Blog5- link(rel='stylesheet', href='/style.css')5+ link(rel='stylesheet', href=base + '/style.css')6 body7 #container!= bodymodified:examples/blog/app.js+24 added-6 removed
7 unmodified lines8910111212131415161718192021222324252627222829303132333435363738394041428 unmodified lines5152535439405556575842435960617 unmodified lines
var express = require('../../lib/express') , messages = require('express-contrib/messages');
app = express.createServer();var app = module.exports = express.createServer();
// Config
app.set('views', __dirname + '/views');app.set('view engine', 'jade');
// mount hook
app.mounted(function(other){ console.log('ive been mounted!');});
// Flash message helper provided by express-contrib// $ npm install express-contrib
app.dynamicHelpers({ messages: messages });app.dynamicHelpers({ messages: messages , base: function(){ // return the app's mount-point // so that urls can adjust. For example // if you run this example /post/add works // however if you run the mounting example // it adjusts to /blog/post/add return '/' == app.route ? '' : app.route; }});
// Middleware
app.configure(function(){8 unmodified lines});
// Routes
require('./routes/site');require('./routes/post');require('./routes/site')(app);require('./routes/post')(app);
if (!module.parent) {app.listen(3000);console.log('Express started on port 3000'); app.listen(3000); console.log('Express started on port 3000');}No newline at end of fileadded:examples/mounting/app.js+23 added
1234567891011121314151617181920212223
// Expose modules in ./support for demo purposesrequire.paths.unshift(__dirname + '/../../support');
/** * Module dependencies. */
var express = require('../../lib/express') , blog = require('../blog/app');
var app = express.createServer();
app.use(express.cookieParser());app.use(express.session({ secret: 'keyboard cat' }));app.use('/blog', blog);
app.get('/', function(req, res){ res.send('Visit <a href="/blog">/blog</a>');});
app.listen(3000);console.log('Server listening on port 3000');No newline at end of filemodified:lib/response.js+15 added-4 removed
15 unmodified lines16 , utils = connect.utils17 , parseRange = require('./utils').parseRange18 , res = http.ServerResponse.prototype19 , send = connect.static.send20+ , join = require('path').join21 , mime = require('mime');22 23 /**24 * Send a response with the given `body` and optional `headers` and `status` code.269 unmodified lines294 * app.get('/post/:id', function(req, res){295 * res.redirect('comments');296 * });297 *298+ * Unless an absolute `url` is given, the app's mount-point299+ * will be respected. For example if we redirect to `/posts`,300+ * and our app is mounted at `/blog` we will redirect to `/blog/posts`.301+ *302 * @param {String} url303 * @param {Number} code304 * @api public305 */306 307 res.redirect = function(url, status){303- var basePath = this.app.set('home') || '/'308+ var app = this.app309+ , base = app.set('home') || '/'310 , status = status || 302311 , body;312 313 // Setup redirect map314 var map = {309- back: this.req.header('Referrer', basePath)310- , home: basePath315+ back: this.req.header('Referrer', base)316+ , home: base317 };318 319 // Support custom redirect map320 map.__proto__ = this.app.redirects;321 322 // Attempt mapped redirect317- var mapped = typeof map[url] === 'function'323+ var mapped = 'function' == typeof map[url]324 ? map[url](this.req, this)325 : map[url];326 327 // Perform redirect328 url = mapped || url;329 330+ // Respect mount-point331+ if (app.route && !~url.indexOf('://')) {332+ url = join(app.route, url);333+ } 334+335 // Support text/{plain,html} by default336 if (this.req.accepts('html')) {337 body = '<p>' + http.STATUS_CODES[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';338 this.header('Content-Type', 'text/html');25 unmodified linesmodified:support/connectnot shown
Its text is not shown, modified.