Skip to content

Commit

Permalink
Rename req.pathname to req.path
Browse files Browse the repository at this point in the history
This is more in line with other Node frameworks and generally just more intuitive.

The original intent was for `req.path` to reference the HTTP/2 ':path' header, but in practice it's more intuitive for it to reference the URL path without the query string.
  • Loading branch information
nwoltman committed Nov 25, 2018
1 parent 20cd75e commit 178e692
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
18 changes: 7 additions & 11 deletions docs/Request.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ app.get('/user/:id', function(req, res) {
+ [`.origin`](#reqorigin)
+ [`.params`](#reqparams)
+ [`.path`](#reqpath)
+ [`.pathname`](#reqpathname)
+ [`.protocol`](#reqprotocol)
+ [`.query`](#reqquery)
+ [`.querystring`](#reqquerystring)
Expand Down Expand Up @@ -90,7 +89,7 @@ The request host (domain) name (the [`host`](#reqhost) without the `port`).
req.hostname // 'www.example.com'

// IPv6 example
req.hostname // [::1]
req.hostname // '[::1]'
```

### `req.href`
Expand Down Expand Up @@ -136,28 +135,25 @@ app.get('/path/:user/:foo', (req, res) => {

### `req.path`

Alias for [`req.url`](#requrl).

### `req.pathname`

*Read-only*

The request pathname (the [URL](#requrl) without the [query string](#reqquerystring)).
The [pathname](https://nodejs.org/dist/latest/docs/api/url.html#url_url_pathname)
part of the request URL.

```js
// URL: /status/user?name=medley
req.pathname // '/status/user'
req.path // '/status/user'
```

### `req.protocol`

*Read-only*

The request protocol ("http" or "https"). Supports `X-Forwarded-Proto` when
the [`trustProxy`](Medley.md#trustproxy) setting is enabled.
The request protocol (e.g. "http" or "https"). Supports reading the `X-Forwarded-Proto`
header when the [`trustProxy`](Medley.md#trustproxy) setting is enabled.

```js
req.protocol // 'http'
req.protocol // 'https'
```

### `req.query`
Expand Down
3 changes: 1 addition & 2 deletions lib/Request.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const RequestPrototype = {
},
},

pathname: {
path: {
get() {
const {url} = this.stream
const qsIndex = url.indexOf('?')
Expand Down Expand Up @@ -110,5 +110,4 @@ const RequestPrototype = {

// Aliases
RequestPrototype.authority = RequestPrototype.host
RequestPrototype.path = RequestPrototype.url
RequestPrototype.scheme = RequestPrototype.protocol
25 changes: 8 additions & 17 deletions test/Request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,23 +364,14 @@ t.test('req.origin - trustProxy=true', (t) => {
})
})

t.test('req.path is an alias for req.url', (t) => {
t.notEqual(Object.getOwnPropertyDescriptor(Request.prototype, 'path'), undefined)
t.strictDeepEqual(
Object.getOwnPropertyDescriptor(Request.prototype, 'path'),
Object.getOwnPropertyDescriptor(Request.prototype, 'url')
)
t.end()
})

t.test('req.pathname - get', (t) => {
t.equal(new Request({url: '/'}).pathname, '/')
t.equal(new Request({url: '/no-query'}).pathname, '/no-query')
t.equal(new Request({url: '/?'}).pathname, '/')
t.equal(new Request({url: '/path?'}).pathname, '/path')
t.equal(new Request({url: '/path?search=1'}).pathname, '/path')
t.equal(new Request({url: '/with/multi/parts?qu?ery'}).pathname, '/with/multi/parts')
t.equal(new Request({url: '/trailing/slash/??query'}).pathname, '/trailing/slash/')
t.test('req.path - get', (t) => {
t.equal(new Request({url: '/'}).path, '/')
t.equal(new Request({url: '/no-query'}).path, '/no-query')
t.equal(new Request({url: '/?'}).path, '/')
t.equal(new Request({url: '/path?'}).path, '/path')
t.equal(new Request({url: '/path?search=1'}).path, '/path')
t.equal(new Request({url: '/with/multi/parts?qu?ery'}).path, '/with/multi/parts')
t.equal(new Request({url: '/trailing/slash/??query'}).path, '/trailing/slash/')
t.end()
})

Expand Down

0 comments on commit 178e692

Please sign in to comment.