0.4.10 • Published 4 years ago

@t-box/server v0.4.10

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

@t-box/server

The T-box server is a node server to handle website requests, its main purpose is to use attributes to define routes to be used by the server. The server works with controllers that are housed in a folder or set of folders and can be registered and the server will find them and process them into the system. The main benefit of this is to making new controllers and separating out different functionality in the site a little easier, some other things that the server allows is for injection so that different controllers can interact with each other without one not knowing about the other so that areas of the site can be sectioned off.

Features

  • Controller structure to simplify routes.
  • Easy attribute usage to register routes on the server.
  • Use of promises by the server to be versitile.
  • Simple parameters to try to parse out the body for easy translation from client to server.

Installation

NPM

    npm install @t-box/server

Yarn

    yarn install @t-box/server

Usage

The following is a simple usage of creating a server that will be listening on localhost:8080.

todo.controller.ts

    import { Controller, Route, Get, Post, Delete, Body } from '@t-box/server';
    import { TodoService, ITodo } from '../services/TodoService';

    @Route('todo')
    export class TodoController extends Controller {
        public constructor(private _service: TodoService) {
            super();
        }

        @Get()
        public search(term: string) {
            return this._service.todos.filter(x => term === undefined || term.length === 0 || x.name.startsWith(term));
        }

        @Get('{0}')
        public get(id: number) {
            let found = this._service.todos.filter(x => x.id === id);
            return found.length > 0 ? found[0] : null;
        }

        @Post('{0}/[action]')
        public done(id: number) {
            let found = this._service.todos.filter(x => x.id === id);
            if (found.length > 0) {
                found[0].done = true;
                return found[0];
            }
            return null;
        }

        @Post()
        public save(id: number, @Body todo: ITodo) {
            let found = this._service.todos.filter(x => x.id === id);
            if (found.length > 0) {
                found[0].done = todo.done;
                found[0].name = todo.name;
                return found[0];
            } else {
                return this._service.add(todo);
            }
        }

        @Delete('{0}')
        public remove(id: number) {
            return this._service.remove(id);
        }
    }

server.ts

    import { Application } from '@t-box/server';
    import { ServiceHandler } from './services/ServiceHandler';

    let app = new Application(__dirname, new ServiceHandler());
    async function boot() {
        await app.register('controllers');
        app.listen(8080);
    }

    boot()
        .then(() => { 
            console.log('Server started on http://localhost:8080');
        })
        .catch(err => {
            console.error(err);
            app.close(() => {
                process.exit();
            });
        });

API

You can read further by using the documentation, it will go into further detail on what options you have when creating the server and using it. Continue Reading

  • Development can be discussed in github issues, be sure to attach the server label.

License

@t-box/server is licensed under the MIT license.

0.4.10

4 years ago

0.4.9

4 years ago

0.4.8

4 years ago

0.4.7

4 years ago

0.4.6

4 years ago

0.4.4

4 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago