Coder Social home page Coder Social logo

humble-server's Introduction

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!

humble-server's People

Contributors

stephenkingsley avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.