|
18 | 18 | npm install aureolin |
19 | 19 | ``` |
20 | 20 |
|
21 | | -## Key Features |
| 21 | +## Getting Started |
22 | 22 |
|
23 | | -- Decorator based routing |
24 | | -- Highly configurable & scalable |
25 | | -- Server Side Rendering |
26 | | -- Strongly Typed |
| 23 | +To get started with Aureolin, you need to create a config file named `aureolin.config.js` in the root of your project. |
| 24 | + |
| 25 | +```js |
| 26 | +/** @filename aureolin.config.js */ |
27 | 27 |
|
| 28 | +const { join } = require('path') |
28 | 29 |
|
29 | | -## Getting Started |
| 30 | +/** @type {import('aureolin').Config} */ |
| 31 | +module.exports = { |
| 32 | + root: join(process.cwd(), process.env.NODE_ENV === 'production' ? 'dist' : 'src') |
| 33 | + port: 4000 |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Then, use the `create` function to create the app. |
| 38 | + |
| 39 | + |
| 40 | +```TS |
| 41 | +/** @filename src/main.ts */ |
| 42 | + |
| 43 | +import { create } from 'aureolin' |
| 44 | +// import create from 'aureolin' |
| 45 | +// const { create } from require('aureolin') |
| 46 | + |
| 47 | +const main = async () => { |
| 48 | + const app = await create() |
| 49 | +} |
| 50 | +``` |
30 | 51 |
|
31 | | -- [Getting Started](https://github.com/alensaito1/aureolin/blob/master/docs/Getting-Started.md) |
32 | | -- [Starter Project](https://github.com/AlenSaito1/Aureolin-starter) |
33 | | -- [Using JSX](https://github.com/alensaito1/aureolin/blob/master/docs/Using-JSX.md) |
| 52 | +### Controllers |
34 | 53 |
|
35 | | -## Documentation |
| 54 | +To create a controller you need to import the `Controller` decorator from Aureolin. |
36 | 55 |
|
37 | | -Not yet available |
| 56 | +Decorators for all Http methods are provided |
| 57 | + |
| 58 | +The return value of the methods prefixed with an Http Decorator in a controller are sent as the response |
| 59 | +Response can be a string, number, object, array, or even a JSX Element (YES! JSX See [Here](https://github.com/alensaito1/aureolin/blob/master/docs/Using-JSX.md)) |
| 60 | + |
| 61 | +Use the provided Parameter Decorators to let Aureolin to know what parameters pass to the to the controller methods. |
| 62 | + |
| 63 | +```TSX |
| 64 | +/** @filename src/controllers/hello.tsx */ |
| 65 | +import { Controller, Context, Get, Ctx, Param } from 'aureolin' |
| 66 | + |
| 67 | +@Controller('/hello') |
| 68 | +export class HelloController { |
| 69 | + @Get('/') |
| 70 | + async hello() { |
| 71 | + return ( |
| 72 | + <div> |
| 73 | + <h1>Hello World!</h1> |
| 74 | + <p>This is a simple example of Aureolin</p> |
| 75 | + </div> |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + @Get('/:name') |
| 80 | + async helloName(@Ctx() { params: { name } }: Context) { |
| 81 | + return `Hello ${name}!` |
| 82 | + } |
| 83 | + |
| 84 | + @Get('/:name/:age') |
| 85 | + async helloNameAge(@Param('name') name: string, @Param('age') age: string) { |
| 86 | + return `Hello ${name}! You are Probably ${age} years old.` |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Providers |
| 92 | + |
| 93 | +Providers are a way to inject dependencies into your controllers. |
| 94 | +Use the `provider` decorator to decalre a class as a provider. |
| 95 | +Use the `inject` decorator to inject provider into your controllers. |
| 96 | + |
| 97 | +`@Provider(provide: string)` |
| 98 | +```TS |
| 99 | +/** @filename src/providers/time.ts */ |
| 100 | +import { Provider } from 'aureolin' |
| 101 | + |
| 102 | +@Provider('time') |
| 103 | +export default class TimeProvider { |
| 104 | + async getTime() { |
| 105 | + return new Date().toLocaleTimeString() |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | +`@Inject(provide: string)` |
| 110 | +```TS |
| 111 | +/** @filename src/controllers/time.ts */ |
| 112 | +import { Controller, Context, Get, Inject } from 'aureolin' |
| 113 | +import type TimeProvider from 'providers/time.ts' |
| 114 | + |
| 115 | +@Controller('/time') |
| 116 | +export class TimeController { |
| 117 | + |
| 118 | + constructor(@Inject('time') private timeProvider: TimeProvider) {} |
| 119 | + |
| 120 | + @Get('/') |
| 121 | + async getTime() { |
| 122 | + return timeProvider.getTime() |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Middlewares |
| 128 | + |
| 129 | +Use the `@Middleware` and `@ControllerMiddleware` Decorator factories |
| 130 | + |
| 131 | +```TS |
| 132 | +import { Middleware, Controller, ControllerMiddleware, Context, Get, Ctx, Param, NextFunction } from 'aureolin' |
| 133 | + |
| 134 | +@Contoller('cakes') |
| 135 | +@ContollerMiddleware([ |
| 136 | + (ctx: Context, next: Next) => { |
| 137 | + console.log('Controller Middleware 1') |
| 138 | + return next() |
| 139 | + } |
| 140 | +]) |
| 141 | +export default class CakesController { |
| 142 | + |
| 143 | + @Get('/cheesecake') |
| 144 | + @Middleware([cheesecakemiddleware]) |
| 145 | + public cheesecake() { |
| 146 | + return { |
| 147 | + name: 'Cheesecake', |
| 148 | + ingredients: ['cheese', 'flour', 'sugar'] |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + @Get('/redvelvet') |
| 153 | + @Middleware([redvelvetmiddleware]) |
| 154 | + public redvelvel() { |
| 155 | + return { |
| 156 | + name: 'Red Velvet', |
| 157 | + ingredients: ['cheese', 'flour', 'sugar', 'red stuff'] |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## Error Handling |
| 164 | + |
| 165 | +Aureolin takes care of error handling for you. |
| 166 | +Use the exported `Exception` or Prebuilt classes to throw error dircetly in your code and aureolin will handle it. |
| 167 | + |
| 168 | +```ts |
| 169 | +/** @filename src/controllers/error.ts */ |
| 170 | +import { Controller, Context, Get, Exception, BadRequestException } from 'aureolin' |
| 171 | + |
| 172 | +@Controller('/error') |
| 173 | +export class ErrorController { |
| 174 | + @Get('/') |
| 175 | + async error(): never { |
| 176 | + throw new Exception('Something went wrong!', 500) |
| 177 | + } |
| 178 | + |
| 179 | + @Get('/:name') |
| 180 | + async errorName(@Ctx() { params: { name } }: Context): never { |
| 181 | + if (!name) throw new BadRequestException(`${name} is not a valid name!`) |
| 182 | + } |
| 183 | +} |
| 184 | +``` |
| 185 | +---- |
| 186 | + |
| 187 | +And finally you can call `create()` to start your app. |
| 188 | + |
| 189 | +```TS |
| 190 | +/** @filename src/main.ts */ |
| 191 | + |
| 192 | +import { create } from 'aureolin' |
| 193 | +const main = async () => { |
| 194 | + const app = await create() |
| 195 | +} |
| 196 | + |
| 197 | +main() |
| 198 | +``` |
| 199 | + |
| 200 | +After you have created the application, Visit ```http://localhost:3000/hello``` to see the output of the `hello` controller. |
| 201 | + |
| 202 | + |
| 203 | +## Key Features |
| 204 | + |
| 205 | +- Decorator based routing |
| 206 | +- Highly configurable & scalable |
| 207 | +- Server Side Rendering |
| 208 | +- Strongly Typed |
38 | 209 |
|
39 | 210 | ## Contributing |
40 | 211 |
|
|
0 commit comments