npm package: @febbyjs/febby
Description: microservice framework for building application services
Author: Vasu Vanka
Latest version: 0.2.8

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

### Table of Contents

-   [Febby][1]
    -   [Parameters][2]
    -   [Examples][3]
    -   [route][4]
        -   [Parameters][5]
        -   [Examples][6]
    -   [routes][7]
        -   [Parameters][8]
        -   [Examples][9]
    -   [middleware][10]
        -   [Parameters][11]
        -   [Examples][12]
    -   [middlewares][13]
        -   [Parameters][14]
        -   [Examples][15]
    -   [router][16]
        -   [Parameters][17]
        -   [Examples][18]
    -   [connection][19]
        -   [Parameters][20]
        -   [Examples][21]
    -   [crud][22]
        -   [Parameters][23]
        -   [Examples][24]
    -   [models][25]
        -   [Examples][26]
    -   [model][27]
        -   [Parameters][28]
        -   [Examples][29]
    -   [bootstrap][30]
        -   [Parameters][31]
        -   [Examples][32]
    -   [expressApp][33]
        -   [Examples][34]
    -   [shutdown][35]
        -   [Examples][36]
    -   [closeConnection][37]
        -   [Examples][38]

## Febby

Febby

### Parameters

-   `config` **[Object][39]** Represents basic app setup. (optional, default `{}`)

### Examples

```javascript
const config = {
  port: 3000,
  hostname: 'abc.xyz',
  db: {
         url: 'mongodb://localhost/test'
      },
  bodyParser: {},
  cors: {},
  helmet: {},
  version: 'v1'
};

const Febby = require('febby');
const febby = new Febby(config);
febby.bootstrap();
```

### route

Register Route

#### Parameters

-   `routeConfig` **[Object][39]** the route object used to create route configuration.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');

 febby.route({
     router: api,
     method: 'get',
     path: '/',
     middlewares: [],
     handler: (req, res, next) => {
         // do business 
     }
 });

 febby.bootstrap(()=>{
     console.log('app started');
 })
```

### routes

Register Route list

#### Parameters

-   `routes` **[Array][40]** list of route objects

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const routes = [
     {
         router: api,
         method: 'get',
         path: '/',
         middlewares: [],
         handler: (req, res, next) => {
             // do business 
         }
      },
     {
         router: api,
         method: 'get',
         path: '/echo',
         middlewares: [],
         handler: (req, res, next) => {
             res.json({echo:req.url});
         }
      }
     ];
febby.routes(routes);

 febby.bootstrap(()=>{
     console.log('app started');
 })
```

### middleware

Register middleware.

#### Parameters

-   `middleware` **[Function][41]** Represents middleware function.
-   `router` **[Object][39]** Represents Express Router object by default it is app router object.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = (req,res,next)=>{
     // some validation logic
     next();
 }
 // middleware runs on api router
 febby.middleware(fn,api);

 // middleware runs on app router
 febby.middleware(fn);
```

### middlewares

Register Middlewares.

#### Parameters

-   `middlewares` **[Array][40]&lt;[Object][39]>** Represents list of middlewares.
-   `router` **[Object][39]** Represents Express Router Object, default to app router.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = (req,res,next)=>{
     // some validation logic
     next();
 }
 // middleware list runs on api router and router is optional
 febby.middlewares([fn,fn2],api);
```

### router

Register a Router.

#### Parameters

-   `url` **[string][42]** Represents url.
-   `router` **[Object][39]** Represents Express Router object, default to app router object.
-   `options` **[Object][39]** Represents Express Router config options.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api'); // path =  /api
 // users subroute mounted on /api
 const userApi = febby.router('/users',api); //  path = /api/users

 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = (req,res,next)=>{
     // some validation logic
     next();
 }
 // this middleware list runs on api router
 febby.middlewares([fn, fn2], userApi);

 // this middleware runs on app router
 febby.middleware(fn);

 febby.route({
     router: userApi,
     method: 'get',
     path: '/',
     middlewares: [],
     handler: (req, res, next) => {
         // do business 
     }
 });

 febby.bootstrap(()=>{
     console.log('app started');
 })
```

### connection

Establish Connection between app and database.

#### Parameters

-   `url` **[string][42]** Represents database url.
-   `options` **[Object][39]** Represents mongoose connect optional object.

#### Examples

```javascript
const config = {
  port: 3000,
  hostname: 'abc.xyz',
  bodyParser: {},
  cors: {},
  helmet: {},
  version: 'v1'
}
 const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = async (req,res,next)=>{
     const db = febby.model(); // Returns db models object
     const users = await db.user.find({});
     // some validation logic on users
     next();
 }
 // this middleware runs on api router
 febby.middleware(fn,api);

 // this middleware runs on app router
 febby.middleware(fn);

 // if db config specified then febby will try to connect to database automatically
 // to make database connection externally then use febby.connection

 const options = {
     useNewUrlParser: true,
     // other mongoose options
 }
 const url = 'mongodb://localhost:27017/test';
 febby.connection(url, options)

 febby.route({
     router: api,
     method: 'get',
     path: '/:id',
     middlewares: [],
     handler: async (req, res, next) => {
         const user = febby.Model('user'); // get registered model by name
         const info = await user.findById(req.params.id);
         res.status(200).send(info);
     }
 });

// start the app 
 febby.bootstrap(()=>{
     console.log('app started');
 })
```

### crud

Creates CRUD on given route object with specific config.

#### Parameters

-   `path` **[string][42]** Represents url path.
-   `config` **[Object][39]** Represents CRUD configuration.
-   `model` **[Object][39]** Represents Model config object.
-   `router` **[Object][39]** Represents Express Router object. it is optional.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const bookApi = febby.router('/books',api);
 const bookCrudConfig = {
   crud: true,
   middlewares:[]
 };
 const bookSchema = {
   name: { type: String },
   author:{ type: Number }
 };
 const bookModel = febby.model('books',bookSchema);
 // creates POST method on book api
 // if model already registered then use febby.model('books') to get model object
febby.crud('/', bookCrudConfig, bookModel, bookApi);
```

### models

Returns models object.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = async (req,res,next)=>{
     const db = febby.model(); // Returns db models object
     const users = await db.user.find({});
     // some validation logic on users
     next();
 }
 // this middleware runs on api router
 febby.middleware(fn,api);

 // this middleware runs on app router
 febby.middleware(fn);

 febby.route({
     router: api,
     method: 'get',
     path: '/',
     middlewares: [],
     handler: (req, res, next) => {
         // do business 
     }
 });

 febby.bootstrap(()=>{
     console.log('app started');
 })
```

Returns **[Object][39]** Returns mongoose models object.

### model

Rigister and Returns model object.

#### Parameters

-   `name` **[string][42]** Represents name of model.
-   `schema` **[Object][39]** Represents mongoose schema object.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = async (req,res,next)=>{
     const db = febby.model(); // Returns db models object
     const users = await db.user.find({});
     // some validation logic on users
     next();
 }
 // this middleware runs on api router
 febby.middleware(fn,api);

 // this middleware runs on app router
 febby.middleware(fn);

 febby.route({
     router: api,
     method: 'get',
     path: '/:id',
     middlewares: [],
     handler: async (req, res, next) => {
         const user = febby.Model('user'); // get registered model by name
         const info = await user.findById(req.params.id);
         res.status(200).send(info);
     }
 });

 febby.bootstrap(()=>{
     console.log('app started');
 })
```

### bootstrap

if database "db" object specified in config then febby will try to connect database on bootstarp.

#### Parameters

-   `fn` **[Function][41]** Represents callback function which will called after app start up.

#### Examples

```javascript
const febby = new Febby(config);
 // create router
 const api = febby.router('/api');
 const fn = (req,res,next)=>{
     // some logic or validation
     next();
 }
 const fn2 = async (req,res,next)=>{
     const db = febby.model(); // Returns db models object
     const users = await db.user.find({});
     // some validation logic on users
     next();
 }
 // this middleware runs on api router
 febby.middleware(fn,api);

 // this middleware runs on app router
 febby.middleware(fn);

 febby.route({
     router: api,
     method: 'get',
     path: '/:id',
     middlewares: [],
     handler: async (req, res, next) => {
         const user = febby.Model('user'); // get registered model by name
         const info = await user.findById(req.params.id);
         res.status(200).send(info);
     }
 });

// start the app 
 febby.bootstrap(()=>{
     console.log('app started');
 })
```

### expressApp

Get Express App object

#### Examples

```javascript
febby.shutdown();
```

### shutdown

Shutdown app

#### Examples

```javascript
febby.shutdown();
```

### closeConnection

Close database connections

#### Examples

```javascript
febby.closeConnection();
```

[1]: #febby

[2]: #parameters

[3]: #examples

[4]: #route

[5]: #parameters-1

[6]: #examples-1

[7]: #routes

[8]: #parameters-2

[9]: #examples-2

[10]: #middleware

[11]: #parameters-3

[12]: #examples-3

[13]: #middlewares

[14]: #parameters-4

[15]: #examples-4

[16]: #router

[17]: #parameters-5

[18]: #examples-5

[19]: #connection

[20]: #parameters-6

[21]: #examples-6

[22]: #crud

[23]: #parameters-7

[24]: #examples-7

[25]: #models

[26]: #examples-8

[27]: #model

[28]: #parameters-8

[29]: #examples-9

[30]: #bootstrap

[31]: #parameters-9

[32]: #examples-10

[33]: #expressapp

[34]: #examples-11

[35]: #shutdown

[36]: #examples-12

[37]: #closeconnection

[38]: #examples-13

[39]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[40]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array

[41]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function

[42]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String