0.0.3 • Published 5 years ago

sfr v0.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Simple, Fast, Reliable(SFR)

  • Expressive HTTP/HTTPS/HTTP2 middleware framework for node.js to make web applications, based on koa core.
  • Partial Code Copying KOA

Installation

sfr requires node v8.9.4 or higher for ES2015 and async function support.

$ npm install sfr

Hello sfr

const sfr = require('sfr');
const app = new sfr({
    serverType: 'http'
});

// response
app.use(ctx => {
  ctx.body = 'Hello sfr';
});

app.start();

defaults params

  • when the server type is http, no certificates need to be set
defaults = {
    httpPort: 80,
    httpsPort: 443,
    serverType: 'https',  // http, https, http2
    certType: 'crt',  // crt, pfx
    cert: path.join(root, 'cert'),  // cert path
    crt: 'default.crt',
    key: 'default.key',
    pfx: 'default.pfx',
    passphrase: 'sample',
    onError: function(ctx){   // custom error
        return new TypeError('Server error. ' + ctx.res.statusCode)
    },
    respond: function (ctx) {  // custom respond
        ctx.res.end(ctx.body);
    }
};

const app = new sfr({
    httpPort: 80,
    httpsPort: 443,
    serverType: 'https',  // http, https, http2
    certType: 'crt',  // crt, pfx
    cert: path.join(root, 'cert1'),  // cert path
    crt: 'default1.crt',
    key: 'default1.key',
    pfx: 'default1.pfx',
    passphrase: 'sample',
    onError: function(ctx){   // custom error
        return new TypeError('Server error. ' + ctx.res.statusCode)
    },
    respond: function (ctx) {  // custom respond
        ctx.res.end(ctx.body);
    }
});

Middleware

sfr is a middleware framework that can take two different kinds of functions as middleware:

  • async function
  • common function

Here is an example of logger middleware with each of the different functions:

async functions (node v7.6+)

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

Common function

// Middleware normally takes two parameters (ctx, next), ctx is the context for one request,
// next is a function that is invoked to execute the downstream middleware. It returns a Promise with a then function for running code after completion.

app.use((ctx, next) => {
  const start = Date.now();
  return next().then(() => {
    const ms = Date.now() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
  });
});

Copyright (C) 2019 Jkin.feng. Licensed MIT. For more details, please see LICENSE.

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago