Added MethodOverride
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent db4dcf8 · Browse files · 7 files·+28 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+28 added−8 removed
- tests:+17 added−1 removed
- other:+11 added−7 removed
Provenance
- Commit
938dc06- Tree
027a382- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
db4dcf8
7 files changed+28 added−8 removed
modified:examples/app.js+2 added
1234567891011121314152 unmodified lines
require.paths.unshift("./lib")require('express')
use(require('express/plugins/method-override').MethodOverride)use(require('express/plugins/content-length').ContentLength)use(require('express/plugins/common-logger').Logger)set('views', dirname(__filename) + '/views')
get('/user/:id?', function() { p(Express.server.request.uri) if (param('id')) return 'Viewing user ' + param('id') return 'Your user account'})2 unmodified linesmodified:lib/express/plugins/method-override.js+2 added-2 removed
2 unmodified lines34567878910112 unmodified lines
exports.MethodOverride = Plugin.extend({ on: { request: function(event) { //if (params('__method__')) // event.request. if (param('__method__')) event.request.method = param('__method__').toLowerCase() } }})No newline at end of fileNo newline at end of filemodified:lib/express/core.js+2 added
239 unmodified lines240 var body 241 response.headers = {}242 this.request = request, this.response = response243 this.request.uri.path = normalizePath(this.request.uri.path)244+ this.request.uri.params = parseNestedParams(this.request.uri.params)245+ trigger('request')246 if (typeof (body = Express.router.route(request)) == 'string')247 halt(200, body)248 },249 28 unmodified linesmodified:lib/express/helpers.js+5 added-5 removed
105 unmodified lines106 return path.slice(path.lastIndexOf('.') + 1)107 }108 109 /**110- * Return a placeholder route param keyed 111- * with the given _key_ or null.110+ * Return a param _key_ value or null.111 *112 * @param {string} key113 * @return {string}114 * @api public115 */116 117 exports.param = function(key) {119- return Express.router.params[key]118+ return Express.router.params[key] ||119+ Express.server.request.uri.params[key]120 }121 122 /**123 * Set response header _key_ and _val_ or get99 unmodified lines223 *224 * @param {hash} params225 * @return {hash}226 * @see parseParams() 227- * @api private227+ * @api public228 */229 230-function parseNestedParams(params) {230+var parseNestedParams = exports.parseNestedParams = function(params) {231 var parts, key232 for (key in params)233 if (parts = key.split('['))234 if (parts.length > 1)40 unmodified linesmodified:spec/spec.helpers.js+8 added
187 unmodified lines188 get('/product/:name', function(){ return param('name') })189 get('/user/12').body.should.eql '12'190 get('/product/ipod').body.should.eql 'ipod'191 end192+ 193+ it 'should access request.uri.params'194+ get('/user', function(){195+ return param('page') || 'First page'196+ })197+ get('/user').body.should.eql 'First page'198+ get('/user', { uri: { params: { page: '2' }}}).body.should.eql '2'199+ end200 end201 endmodified:spec/spec.node.js+1 added
28 unmodified lines2930313233343528 unmodified lines .exec('spec/spec.collection.js') .exec('spec/spec.plugins.js') .exec('spec/spec.plugins.common-logger.js') .exec('spec/spec.plugins.content-length.js') .exec('spec/spec.plugins.method-override.js')JSpec.run({ formatter: JSpec.formatters.Terminal, failuresOnly: true })JSpec.report()modified:spec/spec.plugins.method-override.js+8 added-1 removed
10 unmodified lines11121314151516171819202122232425262710 unmodified lines it 'should consider __method__ as the HTTP method' put('/user', function(){ return 'updated user' }) post('/user', { __method__: 'put' }).body.should.eql 'updated user' post('/user', { uri: { params: { __method__: 'put' }}}).body.should.eql 'updated user' end it 'should force __method__ to lowercase to conform to internal uses' put('/user', function(){ return 'updated user' }) post('/user', { uri: { params: { __method__: 'PUT' }}}).body.should.eql 'updated user' end end end endendNo newline at end of fileNo newline at end of file