Created
August 6, 2019 13:40
-
-
Save tomfun/0e24b2cfb39040f5b053f2a0e9956b91 to your computer and use it in GitHub Desktop.
TypeScript HTTP Hello world example with docker compose & Basic Auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: '3.7' | |
services: | |
test-node: | |
image: node:10 | |
environment: | |
NODE_ENV: ${NODE_ENV:-dev} | |
PORT: 8080 | |
working_dir: /home/node/app | |
volumes: | |
- ./:/home/node/app | |
ports: | |
- '8181:8080' | |
command: "node -r ts-node/register src.ts" | |
test-nginx: | |
image: quay.io/dtan4/nginx-basic-auth-proxy:latest | |
ports: | |
- 8182:80 | |
environment: | |
- BASIC_AUTH_USERNAME=${NGINX_HTTP_USER:-user} | |
- BASIC_AUTH_PASSWORD=${NGINX_HTTP_PASSWORD:-password} | |
- PROXY_PASS=http://test-node:8080/ | |
depends_on: | |
- test-node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "basic-auth-test", | |
"license": "MIT", | |
"dependencies": { | |
"@nestjs/common": "^6.1.1", | |
"@nestjs/core": "^6.1.1" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller, Get, HttpCode, HttpModule, MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; | |
import { NestFactory } from '@nestjs/core'; | |
@Controller('test') | |
export class TestController { | |
@Get('/401') | |
@HttpCode(401) | |
public async bad() { | |
return Math.random(); | |
} | |
@Get('/200') | |
@HttpCode(200) | |
public async good() { | |
return Math.random(); | |
} | |
} | |
@Module({ | |
imports: [ | |
HttpModule, | |
], | |
controllers: [ | |
TestController, | |
], | |
providers: [], | |
}) | |
export class TestModule implements NestModule { | |
configure(consumer: MiddlewareConsumer): MiddlewareConsumer | void { | |
return undefined; | |
} | |
} | |
async function bootstrap() { | |
const app = await NestFactory.create(TestModule); | |
const p = await app.listen(process.env.PORT); | |
function exitHandler() { | |
p.unref(); | |
} | |
// do something when app is closing | |
process.on('exit', exitHandler); | |
// catches ctrl+c event | |
process.on('SIGINT', exitHandler); | |
process.on('SIGTERM', exitHandler); | |
// catches "kill pid" (for example: nodemon restart) | |
process.on('SIGUSR1', exitHandler); | |
process.on('SIGUSR2', exitHandler); | |
// catches uncaught exceptions | |
process.on('uncaughtException', exitHandler); | |
} | |
bootstrap() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment