GithubHelp home page GithubHelp logo

fossabot / aureolin Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alenvelocity/aureolin

0.0 1.0 0.0 2.21 MB

Aureolin is a fast and powerful Web Application Framework

Home Page: https://www.npmjs.com/package/aureolin

License: MIT License

TypeScript 100.00%

aureolin's Introduction

Aureolin

Aureolin is a simple, fast, and powerful REST API framework for Node.js with a focus on simplicity and performance. Built With Koa

NPM FOSSA Status

NPM Discord CodeFactor NPMPrettier


Installation

npm install aureolin

Getting Started

To get started with Aureolin, you need to create a config file named aureolin.config.js in the root of your project.

/** @filename aureolin.config.js */

const { join } = require('path')

/** @type {import('aureolin').Config} */
module.exports = {
    root: join(process.cwd(), process.env.NODE_ENV === 'production' ? 'dist' : 'src')
    port: 4000
}

Then, use the create function to create the app.

/** @filename src/main.ts */

import { create } from 'aureolin'
// import create from 'aureolin'
// const { create } from require('aureolin')

const main = async () => {
    const app = await create()
}

Controllers

To create a controller you need to import the Controller decorator from Aureolin.

Decorators for all Http methods are provided

The return value of the methods prefixed with an Http Decorator in a controller are sent as the response Response can be a string, number, object, array, or even a JSX Element (YES! JSX See Here)

Use the provided Parameter Decorators to let Aureolin to know what parameters pass to the to the controller methods.

/** @filename src/controllers/hello.tsx */
import { Controller, Context, Get, Ctx, Param } from 'aureolin'

@Controller('/hello')
export class HelloController {
    @Get('/')
    async hello() {
        return (
            <div>
                <h1>Hello World!</h1>
                <p>This is a simple example of Aureolin</p>
            </div>
        )
    }

    @Get('/:name')
    async helloName(@Ctx() { params: { name } }: Context) {
        return `Hello ${name}!`
    }

    @Get('/:name/:age')
    async helloNameAge(@Param('name') name: string, @Param('age') age: string) {
        return `Hello ${name}! You are Probably ${age} years old.`
    }    
}

Providers

Providers are a way to inject dependencies into your controllers. Use the provider decorator to decalre a class as a provider. Use the inject decorator to inject provider into your controllers.

@Provider(provide: string)

/** @filename src/providers/time.ts */
import { Provider } from 'aureolin'

@Provider('time')
export default class TimeProvider {
    async getTime() {
        return new Date().toLocaleTimeString()
    }
}

@Inject(provide: string)

/** @filename src/controllers/time.ts */
import { Controller, Context, Get, Inject } from 'aureolin'
import type TimeProvider from 'providers/time.ts'

@Controller('/time')
export class TimeController {

    constructor(@Inject('time') private timeProvider: TimeProvider) {}

    @Get('/')
    async getTime() {
        return timeProvider.getTime()
    }
}

Middlewares

Use the @Middleware and @ControllerMiddleware Decorator factories

import { Middleware, Controller, ControllerMiddleware, Context, Get, Ctx, Param, NextFunction } from 'aureolin'

@Contoller('cakes')
@ContollerMiddleware([
    (ctx: Context, next: Next) => {
        console.log('Controller Middleware 1')
        return next()
    }
])
export default class CakesController {

    @Get('/cheesecake')
    @Middleware([cheesecakemiddleware])
    public cheesecake() {
        return {
            name: 'Cheesecake',
            ingredients: ['cheese', 'flour', 'sugar']
        }
    }

    @Get('/redvelvet')
    @Middleware([redvelvetmiddleware])
    public redvelvel() {
        return {
            name: 'Red Velvet',
            ingredients: ['cheese', 'flour', 'sugar', 'red stuff']
        }
    }
}

Error Handling

Aureolin takes care of error handling for you. Use the exported Exception or Prebuilt classes to throw error dircetly in your code and aureolin will handle it.

/** @filename src/controllers/error.ts */
import { Controller, Context, Get, Exception, BadRequestException } from 'aureolin'

@Controller('/error')
export class ErrorController {
    @Get('/')
    async error(): never {
        throw new Exception('Something went wrong!', 500)
    }

    @Get('/:name')
    async errorName(@Ctx() { params: { name } }: Context): never {
        if (!name) throw new BadRequestException(`${name} is not a valid name!`)
    }
}

And finally you can call create() to start your app.

/** @filename src/main.ts */

import { create } from 'aureolin'
const main = async () => {
    const app = await create()
}

main()

After you have created the application, Visit http://localhost:3000/hello to see the output of the hello controller.

Key Features

  • Decorator based routing
  • Highly configurable & scalable
  • Server Side Rendering
  • Strongly Typed

Contributing


Thank you for using Aureolin!

License

FOSSA Status

aureolin's People

Contributors

alenvelocity avatar fossabot avatar renovate-bot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.