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

microframework

v0.6.4

Published

Microframework is a minimalistic framework you can use with TypeScript and JavaScript.

Downloads

2,020

Readme

Microframework

Microframework is a simple tool that allows you to execute and configure your modules (called loaders) in a proper order, helping you to organize bootstrap code in your application.

Usage

First, install microframework:

npm i microframework

Second, create a simple "loader" named expressLoader. Loader is a place where you can configure all your modules during microframework bootstrap. All loaders are executed one by one in a sequential order.

import {MicroframeworkSettings} from "microframework";

export function expressLoader(settings: MicroframeworkSettings) {

    // create express app
    const app = express();

    // register all routes, Routes are just routes that should be stored outside of this module
    const routes: any = Routes;
    Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath]));

    // run application to listen on given port
    app.listen(3000);
    
    // your loader also can return a promise and microframework will wait for it in a proper order
}

Create app.ts and bootstrap a microframework and your express loader:

import {bootstrapMicroframework} from "microframework";
import {expressLoader} from "./expressLoader";

bootstrapMicroframework([
    expressLoader
])
    .then(() => console.log("Application is up and running."))
    .catch(error => console.log("Application is crashed: " + error));

That's all. You can do same for other loaders. Take a look on sample to understand better how concept of loaders and their bootstrapping in microframework.

Settings

You can specify additional options to microframework.

  • logo - Logo needs to be used before application launches. To use logo ansi-art node module should be installed.
  • showBootstrapTime - If set to true then framework shows how much time was spend to bootstrap microframework.
  • bootstrapTimeout - Number of milliseconds to wait before framework will bootstrap.

Example of using settings:

import {bootstrapMicroframework} from "microframework";
import {expressLoader} from "./expressLoader";

bootstrapMicroframework({
    config: {
        logo: "MyApp",
        showBootstrapTime: true,
        bootstrapTimeout: 10
    }, 
    loaders: [
        expressLoader,
        // ...
    ]
})
    .then(() => console.log("Application is up and running."))
    .catch(error => console.log("Application is crashed: " + error));

Sharing data across loaders

Sometimes few loaders need to communicate between each other and use shared data. For such purpose you can store the data in settings object passed to each loader and use stored data across all other loaders. For example:

import {MicroframeworkSettings} from "microframework";

export function expressLoader(settings: MicroframeworkSettings) {

    // create express app
    const app = express();

    // register all routes, Routes are just routes that should be stored outside of this loader
    const routes: any = Routes;
    Object.keys(routes).forEach(routePath => app.get(routePath, routes[routePath]));

    // run application to listen on given port
    app.listen(3000);
    
    settings.setData("express_app", app);
}

And another loaders can use data this way:

import {MicroframeworkSettings} from "microframework";

export function socketIoLoader(settings: MicroframeworkSettings) {
    const io = io();
    io.useExpress(settings.getData("express_app"));
}

Application Shutdown

In the case if you want to shutdown running application you need to do following:

import {bootstrapMicroframework} from "microframework";
import {expressLoader} from "./expressLoader";

bootstrapMicroframework({
    config: {
        logo: "MyApp",
        showBootstrapTime: true,
        bootstrapTimeout: 10
    }, 
    loaders: [
        expressLoader,
        // ...
    ]
})
    .then(framework => {
        // do something before shutdown
        
        // and shutdown everything
        return framework.shutdown();
    })
    .then(() => {
        // now everything is turned off
    })
    .catch(error => console.log("Application is crashed: " + error));

All loaders which use resources should release them, for example:

export async function typeormModule(settings: MicroframeworkSettings) {
    const connection = await createConnection({
        driver: {
            type: "mysql",
            host: "localhost",
            username: "test",
            password: "test",
            database: "test"
        }
    });

    settings.onShutdown(() => connection.close()); // closing connection on microframework shutdown
}