mirror of
https://github.com/scratchfoundation/restify-cors-middleware.git
synced 2024-12-21 05:12:22 -05:00
allow regexp for origins
This commit is contained in:
parent
9088f776e5
commit
be2f83d322
3 changed files with 12 additions and 3 deletions
|
@ -31,12 +31,13 @@ server.use(cors.actual)
|
||||||
|
|
||||||
## Allowed origins
|
## Allowed origins
|
||||||
|
|
||||||
You can specify the full list of domains and subdomains allowed in your application:
|
You can specify the full list of domains and subdomains allowed in your application, using strings or regular expressions.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
origins: [
|
origins: [
|
||||||
'http://myapp.com',
|
'http://myapp.com',
|
||||||
'http://*.myapp.com'
|
'http://*.myapp.com',
|
||||||
|
/^https?:\/\/myapp.com(:[\d]+)?$/
|
||||||
]
|
]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,9 @@ exports.create = function (allowedOrigins) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function createMatcher (allowedOrigin) {
|
function createMatcher (allowedOrigin) {
|
||||||
if (allowedOrigin.indexOf('*') === -1) {
|
if (allowedOrigin instanceof RegExp) {
|
||||||
|
return requestOrigin => requestOrigin.match(allowedOrigin)
|
||||||
|
} else if (allowedOrigin.indexOf('*') === -1) {
|
||||||
// simple string comparison
|
// simple string comparison
|
||||||
return requestOrigin => requestOrigin === allowedOrigin
|
return requestOrigin => requestOrigin === allowedOrigin
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -52,4 +52,10 @@ describe('Origin list', function () {
|
||||||
var matcher = originMatcher.create(list)
|
var matcher = originMatcher.create(list)
|
||||||
matcher('http://random-website.com').should.eql(false)
|
matcher('http://random-website.com').should.eql(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('supports regular expressions', function () {
|
||||||
|
var list = ['http://api.myapp.com', /https?:\/\/example.com(:8888)?/]
|
||||||
|
var matcher = originMatcher.create(list)
|
||||||
|
matcher('https://example.com:8888').should.eql(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue