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

microbe.js

v0.1.15

Published

A small Node.js framework for simple routing

Downloads

21

Readme

Microbe.js

Join the chat at https://gitter.im/Aweary/microbe.js Build Status

A small, easy to use Node.js framework.

Installation

npm install microbe.js

Usage

Microbe.js acts very similarly to Express.js in that you require the main package and instantiate your application by invoking it as a function.

Below is a basic example with only one route.


import microbe from 'microbe.js'
const app = microbe(__dirname)

app.route({
  path: '/',
  method: 'GET',
  handler: duplex => duplex.render('index', {content: 'This is content!'})
})

app.on('start', () => {
  console.log(`Application started on port ${app.get('port')}`);
})

app.start(3000);

Currently you must pass __dirname to the microbe function so it can easily locate relative folders/files.

Unlike a vanilla Node server the callback function only takes one argument, duplex.

duplex provides an access point for all request and response methods/accessors you'd need to use. For example, you can get the request method at duplex.method and you can render a response with duplex.render. You can also access the full request and response objects on duplex.req and duplex.res respectively/

duplex simplifies the API and makes sure that the ServerResponse and ClientRequest objects aren't manipulated directly, so if you feel you need to access those objects directly, open an issue and we can get a better API setup.

Routing

Routing is accomplished using the app.route method which accepts an objects used to build the route, or using convenience methods as described below.

app.route lets you declare your routes in a very direct way by explicitly providing the path, method, and handler options.

app.route({
  path: '/documents/:document',
  method: 'GET',
  handler: duplex => {
    let document = duplex.params.document
    duplex.static(`/documents/${document}`)
  }
})

Route parameters are also available on the req object via req.params.

Microbe.js also provides standard convenience methods such as app.get and app.post

let details = getSomeDetailObject()
app.get('/', duplex => duplex.json({ details }))

Rendering

Microbe.js implements consolidate.js which means you can use any of the supported engines. Check their repo for a comprehensive list.

You have to set a rendering engine if you're going to be using the res.render method, otherwise an error will be thrown.

Or you may do so using the app.set method:

import microbe from 'microbe.js'
const app = microbe(__dirname)
app.set('engine', 'handlebars')

Microbe.js will assume that your file extensions will be the same as the engine's name (e.g., handlebars uses [name].handlebars, jade uses [name].jade, etc.). If you need to use a different extension then set the ext value as well.

app.set('ext', 'hbs')

duplex.render takes the file name (minus the extension) as the first argument and the data to render as the second

  res.render('about', {time: Date.now()})

Middlware

Middleware is added to a route using the app.use method. The first argument is the route for which the middleware should be invoked. If no route is provided, then it will be used on all routes.


  app.use('/api', duplex => {
    doSomeLoggingOrSomething(duplex)
  })

Events

The app object does inherit from EventEmitter meaning it can emit events and also listen for events. Currently there are a limited number of internal events, but you may attach a listener to any of them:

kickoff is emitted when the app is being initialized/bootstrapped

route is emitted when a request is routed, and it emits a data object with the signature { path, method, handler }

start is emitted when the application starts, and it emits the port the app started on.

State and configurations

If you need to configure your Microbe.js application, you can use the app.set method for adjusting values in the internal state object. Currently, the only two properties you should really be setting yourself are the 'views' nad 'publicFolder' which, as expected, determine where Microbe.js will look for your views and static files, respectively.

  /* this defaults to 'views' */
  app.set('views', 'templates')

  /* this defaults to 'public'*/
  app.set('publicFolder', 'client')