npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tyboost

v2.0.0

Published

Initialization layer and tools for Node.js applications.

Downloads

25

Readme

Tyboost

Build Status

NPM NPM

Initialization layer and tools for Node.js applications.

Tyboost allows initialization steps to be registered for an application. All these steps will be executed sequentially during startup, after which the application will be ready to run.

Tyboost also provides several helpers to load components such as routes handlers, services with IOC, middlewares, etc.

Table of contents

Installation

$ npm install tyboost

Usage

Tyboost should be applicable to any Node.js application. Currently it has been tested with Express Js which is one of the most common Framework

const express = require('express')
const tyboost = require('tyboost')
const config = require('./application-config.json')
const app = tyboost(express(), config)

This will create an Express Js application with two additional functions: app.register() and app.boot() The first one is use to register a step, (manually or with helpers) and the second one is use to boot the application by executed all registered steps.

The optional config parameter is an object with your own configuration of your application. It will be available in the IOC container and in your Express JS application by calling app.get('config')

Register step

When starting a Node JS application, some steps must be executed sequentially, such as loading routes, creating services, connecting to databases or registering middleware.

Helpers registration

Tyboost provides several helpers, check the helpers section for more informations

app.register(tyboost.services('/path/to/the/services'))
app.register(tyboost.routes('/path/to/the/routes'))
app.register(tyboost.middlewares('/path/to/the/middleware'))

Manually registration

You can register manually a function or an array of function

app.register(() => {
    // Do something here
})

app.register([
    () => {
        // Do something here
    },
    () => {
       // Do something here
    }
 ])

Order of execution

Each step will be executed following the order of registration, BUT ONLY if the step is synchronous or if the step return a promise. An async function like setTimeout will not be execute in the registration flow order (even it can be execute after boot of application)

// First to be register
app.register(() => {
    // This step will be the first to be execute
})

// Second to be register
app.register(() => {
    setTimeout(()=> {
       // This code will be the third to be execute
    }, 1000)
})

// Third to be register
app.register(() => {
    return new Promise(resolve => {
        // This code will be the second to be execute
        resolve()
    })
})

Boot application

Call app.boot to boot your application, this will return a promise. Registered steps will be executed sequentially, and the promise will be resolve when all steps are complete.

Example with the default promise syntax:

app.boot()
   .then(()=>{
       app.listen(8080, () => {
           console.log('Listen on port 8080')
       })
   })
   .catch((err)=>{
       console.log(err)
   })

Example with await/async syntax:

async function start () {
    try {
        await app.boot()
        app.listen(8080, () => {
            console.log('Listen on port 8080')
        })
    } catch(err) {
        console.log(err)
    }
}

start()

Express JS Helpers

Currently the helpers has been design only for Express Js

Routes helper

The routes helpers allows you to load multiple routes from a folder (including the sub folder) at one time. Each file have to return a Express JS router.

You can use this helper with the function tyboost.routes()

Example of a directory structure

.
+-- index.js
+-- src
|   +-- handler
|   |   +-- account
|   |   |   +-- User.js
|   |   +-- Login.js

File User.js:

let router = express.Router()
router.get('/user', (req, res)=>{
    res.json({name:'My User Name'})
})

module.exports = router

File Login.js:

let router = express.Router()
router.post('/login', (req, res)=>{
    // Authentification...
    res.json({ logged:true })
})

module.exports = router

To load the routes call the helper like this:

app.register(tyboost.routes(__dirname + 'src/handler'))

You can add prefix /api a the beginning of your route like this:

app.register(tyboost.routes(__dirname + 'src/handler'), '/api')

You can also call this helper with only with one file

app.register(tyboost.routes(__dirname + 'src/handler/account/User.js'))

The routes helper is design to load multiple routes in same time but the following syntax will do the same thing:

let router = express.Router()
  app.register(() => {
    router.get('/user', (req, res) => {
      res.json({name:'My User Name'})
    })
  app.use('/account', router)
})

Services helper

The services helpers allows you to load multiple services in an IOC (Inversion Of Control) container. This container will be available in a container parameter of the NodeJS application and usable for instance in route handler by calling app.get('container')

You have multiple way to call the the service helper:

Load by file

Each file must return an array (or an object) with a service definition, containing the following parameters:

  • Unique service name (REQUIRED)
  • Definition of Class (REQUIRED)
  • Array of dependencies (OPTIONAL, default: [])
  • A boolean that define if the service is a singleton (OPTIONAL, default: true)

The values in dependencies object the name of an other service or a config parameters of the main configuration.

Example of a simple service:

const Service = class Service {

    constructor (otherService, param1, param2) {
    // Do something
    }

    myFunc() {
    // Do something
    }
}

module.exports = ['service1', Service, ['@service2', '%param1%', '%section1.param2%'], true]

or with an object:

module.exports =  { name: 'service1',
                    definition: Service,
                    dependencies: ['@service2', '%param1%', '%section1.param2%'],
                    singleton: true }

To load the services with a config file:

const express = require('express')
const tyboost = require('tyboost')
const config = {param1: "value1", section1: {param2: "value2"}}

const app = tyboost(express(), config)

app.register(tyboost.services('/path/to/the/services'))

After doing this you can use the container in an route like this:

router.get('/my-route', (req, res) => {
    const container = req.app.get('container').get('service1')
    const service = container.get('service1')
    const result = service.myFunc()

    res.json(result)
})

Load by object

const Service = class Service { constructor (param1){} }
app.register(tyboost.services({name: 'myServiceName', definition: Service, dependencies: ['value1']}))

Load by array of object

const Service = class Service { constructor (param1){} }
app.register(tyboost.services([ {
                                    name: 'myServiceName',
                                    definition: Service,
                                    dependencies: ['value1'],
                                    singleton: false
                                 },
                                 {
                                    name: 'myServiceName2',
                                    definition: Service,
                                    dependencies: ['value2'],
                                    singleton: true
                                 }
                               ]
               ))

Middleware helper

The routes helpers allows you to load multiple middleware from a folder (including the sub folder) at one time. Each file have to return a function.

If you want that your middleware to be execute before route Handler you have to call the middleware helper before loading route. By the way, if you want to register a middleware that catch all error, you have to call the middleware helper after all the other registration.

Example with two middleware:

app.register(tyboost.middlewares('/path/to/the/middleware_pre_handler'))
app.register(tyboost.routes('/path/to/the/routes'))
app.register(tyboost.middlewares('/path/to/the/middleware_post_handler'))

Example

Repository example are available here: https://github.com/electrotiti/tyboost-example

Tests

To launch the test you just have to do:

$ npm install
$ npm test

License

The MIT License

Copyright (c) 2018 Thierry LESZKOWICZ http://github.com/electrotiti