Skip to content

Commit

Permalink
Merge pull request #16 from nwoltman/req-original-url
Browse files Browse the repository at this point in the history
Set `req.originalUrl` before running middleware
  • Loading branch information
mcollina authored Dec 20, 2017
2 parents 1384ad8 + c2400aa commit ee210a0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions middie.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ function middie (complete) {
return
}

req.originalUrl = req.url

var holder = pool.get()
holder.req = req
holder.res = res
holder.url = sanitizeUrl(req.url)
holder.originalUrl = req.url
holder.context = ctx
holder.done()
}
Expand All @@ -53,7 +54,6 @@ function middie (complete) {
this.req = null
this.res = null
this.url = null
this.originalUrl = null
this.context = null
this.i = 0

Expand All @@ -65,7 +65,7 @@ function middie (complete) {
var context = that.context
var i = that.i++

req.url = that.originalUrl
req.url = req.originalUrl

if (err || middlewares.length === i) {
complete(err, req, res, context)
Expand Down
23 changes: 22 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,25 @@ test('should keep the context', t => {
instance.run(req, res, { key: true })
})

test('should add `originalUrl` property to req', t => {
t.plan(2)

const instance = middie(function (err) {
t.error(err)
})
const req = {
url: '/test'
}
const res = {}

instance.use(function (req, res, next) {
t.equal(req.originalUrl, '/test')
next()
})

instance.run(req, res)
})

test('basic serve static', t => {
const instance = middie(function () {
t.fail('the default route should never be called')
Expand Down Expand Up @@ -244,6 +263,7 @@ test('should match all chain', t => {
t.error(err)
t.deepEqual(req, {
url: '/inner/in/depth',
originalUrl: '/inner/in/depth',
undefined: true,
null: true,
empty: true,
Expand Down Expand Up @@ -289,7 +309,8 @@ test('should match the same slashed path', t => {
const instance = middie(function (err, req, res) {
t.error(err)
t.deepEqual(req, {
url: '/path'
url: '/path',
originalUrl: '/path'
})
})
const req = {
Expand Down

0 comments on commit ee210a0

Please sign in to comment.