1.4.2 • Published 6 years ago

inra-server-http v1.4.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

inra-server-http

npm Dependency Status

A wrapper for common Node.js frameworks. Creates a micro application suitable for small applications with low overhead such as RESTful APIS. This class aims to be a better, more expressive, and more robust foundation for web applications and APIs.

Note: full documentation with more examples is published on our Wiki. Please, refer to our Wiki for installation details and API references.

Installation

$ npm install --save inra-server-http

API

const server = new Server({
  port: Number(process.env.PORT)
});

Each Server instance is a Proxy which defines custom behaviors for fundamental operations such as "magic" setters and getters. It allows us to reflect all custom properties to a dependency injector which keeps fundamental object safe and immutable. Gives another layer of abstraction for data.

  • Setters are used to prevent developers from overriding server internals. Additionally, custom-defined properties will be added to an internal Map object which serves as Service Location of services and it's itself a container for them.

  • Getters retrieve those properties from the Map object by key, if such key doesn't exist in server internals. You should avoid to access server internals manually, unless you know what are you doing.

Server

.setEngine(framework)

Sets a engine for server. Each engine must implement methods described in EngineInterface.

Example: using Koa.js

server.setEngine(new Koa());

.setRouter(framework)

Sets a router for server. Each router must implement methods described in RouterInterface.

Example: using koa-router

server.setRouter(new Router());

.import(path)

Imports, initialises and handles a given resource. Resources are recognized based on their name.

Example:

app.import("./path/to/something.js");
// ./path/to/something.js
export default function(app) {
  // …
}

.run(port, callback)

Listens on a given port.

.use(middleware)

Mounts the specified middleware function or functions.

Middlewares

Assuming we are using Koa.js:

import middleware from "inra-server-http/middleware";

@middleware()
export default class SomeMiddleware {
  constructor(app) {
    // …
  }

  async before(ctx, next, ...rest) {
    // …
  }

  async handle(ctx, next, ...rest) {
    // …
  }

  async after(ctx, next, ...rest) {
    // …
  }
}

Controllers

Assuming we are using Koa.js:

import controller, {get, post, patch, del} from "inra-server-http/router";

@controller("/namespace")
export default class SomeController {
  constructor(app) {
    // …
  }

  @post("/resource", function (app) {
    return compose([
      this.isAuthorized(),
      this.hasPermission(["create", …])
    ])
  })
  async create(ctx, next) {
    // …
  }

  @get("/resource/:id")
  async read(ctx, next) {
    // …
  }

  @get("/resource")
  async readAll(ctx, next) {
    // …
  }

  @patch("/resource")
  async update(ctx, next) {
    // …
  }

  @del("/resource")
  async delete(ctx, next) {
    // …
  }
}

Contributing

Bug reporting

Github Open Issues Github Closed Issues Github Pull Requests

We want contributing to Inra Server to be fun, enjoyable, and educational for anyone, and everyone. Changes and improvements are more than welcome! Feel free to fork and open a pull request. If you have found any issues, please report them here - they are being tracked on GitHub Issues.

Development

We have prepared multiple commands to help you develop inra-server-http on your own. Don't forget to install all Node.js dependencies from npm. You will need a local copy of Node.js installed on your machine.

$ npm install

Usage

$ npm run <command>

List of commands

CommandDescription
buildBuilds inra-server-http
watchRe-builds inra-server-http on changes
cleanDeletes builds and cache
lintFixes Lint errors
flowChecks Flow errors
testChecks for style guide errors and runs tests
1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago