DEV Community

Cover image for Check out MicroCosmos.JS: Experimental high-performance HTTP/2 API framework
Goran Brkuljan
Goran Brkuljan

Posted on • Updated on

Check out MicroCosmos.JS: Experimental high-performance HTTP/2 API framework

Hello everyone!

MicroCosmos.js is a Proof of Concept HTTP/2 Node.js API framework. Right now, It supports automatic routing, params, server-sent events and it follows convention over configuration.

GitHub logo nodecosmos / microcosmos

Experimental Node 15 (ES6) HTTP/2 API Framework 🚀🌌

MicroCosmos.JS🚀🌌

Experimental High Performance HTTP/2 API Framework For Node.js 15 (ES6)

Example Microcosmos.js Project with simple Actions and SSE

Get started:

npm install microcosmos.js --save
  • Add "type": "module" in package.json.

  • Generate local certificate for HTTP/2

    openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' -keyout localhost-privkey.pem -out localhost-cert.pem
    Enter fullscreen mode Exit fullscreen mode
  • Create applications actions for shared actions logic /actions/ApplicationsActions.js

    import { MicroAction } from 'microcosmos.js';
    export default class ApplicationActions extends MicroAction {
      // Here we put common logic for our app Action Stores
    }
    Enter fullscreen mode Exit fullscreen mode
  • Sample Action Store: /actions/UserActions.js

    import ApplicationActions from './ApplicationsActions.js';
    
    export default class UserActions extends ApplicationActions {
      static get routeName() {
        return 'users';
      }
    
      show() {
        this.render.json({ message: 'Hello from /users/show' });
      }
    }
    Enter fullscreen mode Exit fullscreen mode
  • Add Following to entry point of app e.g. index.js

    import { readFileSync } from 'fs';
    import
    …
    Enter fullscreen mode Exit fullscreen mode

In just few lines of code write API actions like this:

export default class UserActions extends MicroAction {
  static get routeName() {
    return 'users';
  }

  show() {
    this.render.json({ message: 'Hello from /users/show' });
  }
}
Enter fullscreen mode Exit fullscreen mode

Lib automatically initializes action stores from actions/ folder in the root directory. You can map the action store by using static getter routeName, so in the preceding sample action show() will be mapped to https://localhost:3000/users/show.

Params

Params object can be accessed by using this.params, so if we need urlSearchParams we access it by this.params.searchParams.

Third part of the path is matched as id within params, so if we have path https://localhost:3000/users/show/42 we can access 42 by:

show() {
  this.params.id; // 42
}
Enter fullscreen mode Exit fullscreen mode

On HTTP POST request we can access data object by using this.params.data.

async create() {
  this.params.data; // {}
}
Enter fullscreen mode Exit fullscreen mode

SSE

Microcosmos also supports Server-Sent Events, and those depend on Redis. If Redis has an additional configuration, it should be exported as an object from conifg/redis.js within a project.

To join SSE we use: this.streamer.join('namespace', this.stream) within Action Stores.
To Emit SSE we use: this.streamer.emitEvent('namespace', data)

export default class UserActions extends ApplicationActions {
  static get routeName() {
    return 'users'
  }

  // Join for SSE
  joinEvent() {
    this.streamer.join('user_event', this.stream);
  }

  // Emit SSE
  emitEvent() {
    this.streamer.emitEvent('user_event', this.params.searchParams.get('event'));
    this.endTXT('ok');
  }
}
Enter fullscreen mode Exit fullscreen mode

To listen for events for 'user_event' on client side you do:

// chrome console
const event = new EventSource('/users/joinEvent');
event.onmessage = (event) => console.log(event.data);
Enter fullscreen mode Exit fullscreen mode

and you can go to open https://localhost:3000/users/emitEvent?event=helloFromUserEvent in the new tab, and every time you hit this link it should output "helloFromUserEvent" in the console.

Top comments (0)