Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,17 @@ before_install:

# Setup Node.js version-specific dependencies
- "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul"
- "test $TRAVIS_NODE_VERSION = '0.8' || npm install --save-dev @types/connect @types/node typescript"
- "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)"

# Update Node.js modules
- "test ! -d node_modules || npm prune"
- "test ! -d node_modules || npm rebuild"
script:
# Run test script, depending on istanbul install
- "test ! -z $(npm -ps ls istanbul) || npm test"
- "test -z $(npm -ps ls istanbul) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
- "test ! -z $(npm -ps ls istanbul ) || npm test"
- "test -z $(npm -ps ls istanbul ) || npm run-script test-travis"
- "test -z $(npm -ps ls eslint ) || npm run-script lint"
- "test -z $(npm -ps ls typescript) || npm run-script test-typings"
after_script:
- "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ app.use(vhost('api.example.com', function (req, res) {
app.listen(3000)
```

### Typescript support

Typescript defintions are included in npm package.

## License

[MIT](LICENSE)
Expand Down
24 changes: 24 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as http from "http"

declare namespace vhost {
interface RequestHandler {
(req: http.IncomingMessage, res: http.ServerResponse, next: Function): void
}
}

declare function vhost(hostname: string | RegExp, handle: vhost.RequestHandler): vhost.RequestHandler

declare module "http" {
interface IVHost {
[key: number]: string
host: string,
hostname: string,
length: number
}

interface IncomingMessage {
vhost: IVHost
}
}

export = vhost
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
"files": [
"LICENSE",
"HISTORY.md",
"index.js"
"index.js",
"index.d.ts"
],
"engines": {
"node": ">= 0.8.0"
},
"scripts": {
"lint": "eslint --plugin markdown --ext js,md .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-typings": "tsc -p tsconfig.json",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/",
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/"
}
Expand Down
19 changes: 19 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as vhost from "../index"
import * as connect from "connect"

const app = connect()

// Using string hostname
vhost("*.example.com", app)

// Using regex hostname
vhost(/(.*)\.example.com/, app)

// Using request handler
vhost("*.example.com", (req, res, next) => {
req.vhost[0] === 'foo'
req.vhost[1] === 'bar'
req.vhost.host === 'foo.bar.example.com:8080'
req.vhost.hostname === 'foo.bar.example.com'
req.vhost.length === 2
})
20 changes: 20 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"types": [
"node"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"test/test.ts"
]
}