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

humble-server

v0.3.2

Published

nodejs framework that provide convenient, steady server.

Downloads

3

Readme

humble-server

nodejs framework that provide convenient, steady service.

Example

Installation

humble-server requires node v8 or higher and async function support.

$ npm install humble-server --save

Hello Humble

const HumbleServer = require('humble-server');
const humbleServerApp = new HumbleServer({
  numCPUs: 1,
  port: 6969,
});

humbleServerApp.router.get('/hello', () => 'hello humble');

humbleServerApp.start();

Controller

This is the core for a nodejs app. The developer can design your own logic in this http request.

The rule number one is the controller must be async/await function. The return value will send automatically.

async function home(context) {
  const ret = await context.render('home.html', { title: 'Humble-Server' });
  return ret;
}

module.exports = home;

Router

Now let look back on Hello Humble. Using humbleServerApp.router to register controller router.

Route paths will be translated to regular expressions using path-to-regexp.

Base Apis

humbleServerApp.router[method](path, controller)

  • method: get, post, put, delete, patch.
  • path: url path.
  • controller: controller
const home = require('./controller/home');
humbleServerApp.router.get('/home', home);

humbleServerApp.router.dynamicRouter(method, path)

  • method: http method, such as get, post, put, delete, patch.
  • path: url path.

In this api, Humble will find the controller according to your path.

humbleServerApp.router.dynamicRouter('get', '/v1/api/:path*');

when http.req.url is /v1/api/user/getUserInfo that is match /v1/api/:path. path-to-regexp will return user/getUserInfo that is the value of /:path*. And then Humble will execute ${your project dir}/controller/user/getUserInfo.js.

Middleware

Humble is a middleware framework such as Koa that use async function.

Here is an example of logger middleware

async function log1(context, next) {
  const startTime = new Date().getTime();
  console.log(`--- start middleware log1 url: ${context.req.url} ---`);
  const res = await next();
  console.log(`--- end middleware log1 url: ${context.req.url} dur: ${new Date().getTime() - startTime} ---`);
  return res;
}

module.exports = log1;

Next

In middleware, next function is important that can execute next middleware automatically. So you can design your own logic in middleware.

Plugin

Plugin is another elegance architecture. Create your own plugins in /plugin folder without any config. In the controller you can run context.plugin[${your plugin file name}].

// controller
async function home(context) {
  context.plugin.log.info('--- hello ---');
  return 'hello';
}

module.exports = home;

The rule of plugin is the plugin must be class.

// plugin log
class Log {
  info(str) {
    console.log(str);
  }
}

module.exports = Log;

Config

Config will according to your NODE_ENV to require. But you have to has config.default.js file, this is the default config.

Config file name: config.${NODE_ENV}.js

$ NODE_ENV=production node index.js

Humble will loading config/config.default.js and config/config.production.js

Middleware Config

if you have some middleware to run with any http request, you should defined in config.

module.exports = {
  middleware: [
    'middleware1',
    'middleware2',
  ],
};

View

View use nunjucks. There is a api that name render can using in controller.

// /controller/home.js
async function home(context) {
  const ret = await context.render('home.html', { title: 'Hello Humble-Server' });
  return ret;
}
module.exports = home;

and html template such as

<html>
  <head>
    <title>{{ title }}</title>
  </head>
  <body>
    <h1>hello Humble-Server</h1>
  </body>
</html>

context.render(template, data);

  • template: the html file name, Humble will find this file in view folder.
  • data: the data will pass to html template

Develop

This project provided an example, just run npm run dev!