Expose
@klotho::expose
Specified on a resource to expose that resource to the public.
Supported Libraries
- Express
- NestJs
Annotate an Express listen
call to enable. Note that you must provide a target = "public"
directive, as described below.
Currently supports routes specified on the express.Express
object itself:
const express = require('express');
const app = express();
app.get('/', async (req, res) => { res.send("Hello from klotho!") });
/* @klotho::expose {
* target = "public"
* id = "gateway"
* }
*/
app.listen(3000);
exports.app = app;
Or, on middleware:
const router = express.Router();
router.get('/hello', async (req, res) => { res.send(`Hello ${req.body.user}`) })
app.use(router);
/* @klotho::expose {
* target = "public"
* id = "gateway"
* }
*/
app.listen(3000);
exports.app = app;
Setup
For middleware and other setup that require an asynchronous process, the application may also be a Promise
.
async function setupApp() {
const app = express();
app.use(await createMyMiddleware());
// setup routes, etc
return app;
}
const app = setupApp();
app.then(app => app.listen(3000));
exports.app = app;
Annotate an NestJs listen
call to enable. Note that you must provide a target = "public"
directive, as described below.
note
Currently Klotho only supports NestJs applications compiled using tsc
and NestFactories which use the ExpressAdapter
.
Defining your Nest Application
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ExpressAdapter } from '@nestjs/platform-express';
import * as express from 'express';
async function bootstrap() {
const express = express();
const app = await NestFactory.create(AppModule, new ExpressAdapter(express));
await app.init()
return app
}
const nestApp = bootstrap();
/**
* @klotho::expose {
* id = "nestApp"
* target = "public"
* }
*/
await nestApp.listen(3000);
Currently supports routes specified on objects annotated with the nest @Controller()
decorator.
import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { UsersService } from './users.service';
import { UserModel } from './users.model';
@Controller('users')
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Get()
async getUsers() {
return await this.usersService.findAllUsers()
}
@Post()
async postUser(@Body() user: UserModel) {
return await this.usersService.writeUser(user)
}
@Get('/:id')
async getUser(@Param('id') id: string) {
return await this.usersService.getUser(id);
}
}
Directives
Directive | Type | Description |
---|---|---|
id | string | (Required) The exposed endpoint's id. Changing this results in a new endpoint (URL). |
target | string | (Required) Must be set and the only allowed value is "public" |
Providers
- AWS
- GCP
- Creates an API Gateway.
caution
GCP hasn't been updated to v1 yet, so this is just a projection of what it may be.
- Creates an API Gateway.