npm

Need private packages and team management tools?Check out npm Orgs. »

sfr

0.0.3 • Public • Published

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.

install

npm i sfr

Downloadsweekly downloads

1

version

0.0.3

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability