@guseyn/cutie-rest
Cutie extension for using REST (based on cutie-http) in Node.
Last updated 2 years ago by guseyn .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @guseyn/cutie-rest 
SYNC missed versions from official npm registry.

cutie-rest

Cutie extension for using REST (based on cutie-http) in Node.

NPM Version

Cutie extension for fs module in Node. It's based on the Async Tree Pattern.

Usage

const {
  // Needed async objects here from the table below
} = require('@guseyn/cutie-rest');

This library provides following objects: Backend, RestApi, ServingFiles, CachedServingFiles and Method, NotFoundMethod interfaces.

Object Parameters Description
Backend port (number), host (string), api (RestApi) Declares backend server(just http for now) on specified port and host, also it provides declared api (REST).
RestApi ...methods(classes that extend Method) Declares methods of api.
Method regexp (RegExp), method (string) Declares a method(in api) with url that matches regexp and specified method('GET', 'POST', etc.). This class has a method invoke(request, response) that needs to be overridden.
ServingFiles regexp (RegExp), mapper (function(url)), notFoundMethod (Method) Extends Method and serves files on url that mathes regexp with mapper function that gets location of a file on a disk by the url. Also it's required to declare notFoundMethod that handles the cases when a file is not found.
CachedServingFiles regexp (RegExp), mapper (function(url)), notFoundMethod (Method) Does the same that ServingFiles does and caches files for increasing speed of serving them.
NotFoundMethod regexp (RegExp) Method is used in RestApi, ServingFiles, CachedServingFiles for declaring method on 404(NOT_FOUND) status.

Example

'use strict'

const path = require('path');
const {
  Backend,
  RestApi,
  CachedServingFiles
} = require('@guseyn/cutie-rest');
const GeneratedResponse = require('./GeneratedResponse');
const CustomNotFoundMethod = require('./CustomNotFoundMethod');

const notFoundMethod = new CustomNotFoundMethod(new RegExp(/\/not-found/));

const mapper = (url) => {
  let paths = url.split('/').filter(path => path !== '');
  return path.join(...paths);
}

new Backend(8080, '127.0.0.1', new RestApi(
  new GeneratedResponse(new RegExp(/\/response/), 'GET'),
  new CachedServingFiles(new RegExp(/\/files/), mapper, notFoundMethod),
  notFoundMethod
)).run();

CustomNotFoundMethod

'use strict'

const { NotFoundMethod } = require('./NotFoundMethod');

class CustomNotFoundMethod extends NotFoundMethod {

  constructor(regexpUrl) {
    super(regexpUrl);
  }

}

module.exports = CustomNotFoundMethod;

GeneratedResponse

This class also uses cutie-http

'use strict'

const { Method } = require('@guseyn/cutie-rest');

const {
  EndedResponse,
  WrittenResponse,
  ResponseWithWrittenHead
} = require('@guseyn/cutie-http');

class GeneratedResponse extends Method {

  constructor(regexpUrl, type) {
    super(regexpUrl, type);
  }

  invoke(request, response) {
    new EndedResponse(
      new WrittenResponse(
        new ResponseWithWrittenHead(
          response, 200, 'ok',  {
            'Content-Type': 'text/plain' 
          }
        ), 'content ... '
      ), `is delivered`
    ).call();
  }

}

module.exports = GeneratedResponse;

Current Tags

  • 1.0.7                                ...           latest (2 years ago)

8 Versions

  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           2 years ago
  • 1.0.5                                ...           2 years ago
  • 1.0.4                                ...           2 years ago
  • 1.0.3                                ...           2 years ago
  • 1.0.2                                ...           2 years ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (0)
None
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |