0.0.1 • Published 5 years ago

kvick-rest-server v0.0.1

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

Kvick REST Server

A simple Node.js REST API setup using typescript

Getting Started

Make sure you've installed it

npm i -S kvick-rest-server

The library provides two basic classes that can be utilized in order to build a simple Node.js service:

ClassConstructor PropsDescription
AppcontextName of the app and a port number that it should use.
routerRouter instance.
middlewareAdditional middleware that you might want to use. Optional.
RouterroutesAn array of Route Objects.

Body parser is already built in, so you don't have to pass it as additional middleware

Route Objects play a crucial role in this setup and have the following structure:

PropertyTypeDescription
pathstringEg. "/some/path".
verbHTTPVerbs or USEEither GET, POST, PUT, DELETE or USE if you just want to utilize a middleware. (Use HTTPVerbs and MiddlewareVerbs Enums)
handlerRequestHandler or RouterFinal handler. The one that actually sends response. Or another router.
middlewareRequestHandler[]An array of middleware functions. Optional.

Let's go through a series of steps required to build a simple app:

  1. Define a router and pass it an array of Route Objects.
const router = new Router([
  {
    path: 'some/path',
    verb: HTTPVerbs.POST,
    handler: someHandler, 
  }
]);
  1. Define an app, name it and pass the router you defined earlier.
const app = new App({ name: 'My app', port: 3000 }, router);
  1. Run it.
app.run();

Sample Project Structure

├── handlers                # Contains all handlers connected to Router
├── middleware              # Contains all middleware functions (if any)
├── providers               # For sake of simplicity if there's an async action
│   ├── third-party.ts      # Such as third-party api calls
│   ├── my-db.ts            # Or DB operations
│   ├── smth-else.ts        # Let's just call it a provider
├── app.ts                  # Contains basic setup including all routes

That's just a simple example. More complex stuff can be easily implemented as well.

A collection of reusable setups or "mods" that extend basic App class can be added to this library later in order to simplify creation of certain types of services.