npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gacrux-lib-restify

v1.0.0

Published

utils for rstify

Downloads

3

Readme

gacrux-lib-restify

Travis Codecov

restify manager that uses inversify under the hood

For more information about this project, see the main repo

Installation

Currently this is not available on npm. Won take long until it gets there :)

Usage

Before you can boot up your restify server, you need to do some steps.

Step 1: Create a Controller

Every controller class needs the @Controller decorator. Inside the decorator, you need to set the URL. So if you want the class to answer the request on /foo you need to insert /foo. For the response, you need to return an object that matches the Response object from this library. The response object is either an object or a promise. Per default the response status is 200. The data field is required.

This controller would answer on the route: GET /foo with the status 200 and the body containing bar. When using POST the server would answer with the status 200, and the body { 'foo': 'bar' }.

import { Controller, Get, Post, Response } from 'gacrux-lib-restify';
import { injectable } from 'inversify';
import { Rquest } from 'restify';

@injectable()
@Controller('/foo')
export class FooController {

  @Get('/')
  public get(): Response {
    return {
      data: 'bar',
      json: false,
      status: 200
    }
  }

  @Post('/')
  public post(request: Request): Promise<Response> {
    return new Promise<Response>((resolve, reject) => {
      // do something with the request
      // like saving it in the database
      resolve({
        data: {
          foo: 'bar'
        }
      });
    });
  }
}

Step 2: Register the Controller

There two ways. The first method is to self-register the controller in the container. The second method is to add the controller over the manager class. In the end, both methods do exactly the same.

Method 1. The manual way

import { Container, interfaces } from 'inversify';
import { CONTROLLER, ControllerInterface, RestifyManager } from 'gacrux-lib-restify';
import { FooController } from './foo';

const container: interfaces.Container = new Container();

container.bind<ControllerInterface>(CONTROLLER).to(FooController).whenTargetNamed('MyTag');

const restifyManager: RestifyManager = new RestifyManager(container, { port: 8080 });

Method 2. Using the manager class

Using this method the tag is optional. Per default it will take the class name.

import { Container, interfaces } from 'inversify';
import { FooController } from './foo';
import { RestifyManager } from 'gacrux-lib-restify';

const container: interfaces.Container = new Container();
const restifyManager: RestifyManager = new RestifyManager(container, { port: 8080 });

restifyManager.registerController(FooController, 'MyTag');

Step 3: Starting the server

The last step is to start the server.

import { Container, interfaces } from 'inversify';
import { FooController } from './foo';
import { RestifyManager } from 'gacrux-lib-restify';

const container: interfaces.Container = new Container();
const restifyManager: RestifyManager = new RestifyManager(container, { port: 8080 });

restifyManager.registerController(FooController);
restifyManager.init()
.then(() => {
  console.log('Server up and running');
});

API

RestifyManager

This class is used for starting the restify server, adding controllers and shutting down the server.

new RestifyManager(container: Container, options: RestifyOptions)

Creates a new instance of the RestifyManager.

  • container -> inversify container. The library will add all controllers to this container
  • options -> currently only the port is in here. See the examples above to see how it works

.init(): Promise<RestifyManager>

Starts the restify server. Listens to the port, given in the options. Per default the middleware BodyParser is initialized.

Important note: It is recommended to add all controllers BEFORE you start the server.

Gives back a Promise containing an instance of the class.

.cleanup(): Promise<RestifyManager>

Shutsdown the server.

Gives back a Promise containing an instance of the class.

.registerController(clazz [, tag: string]): void

Registeres a new controller.

  • clazz -> simple the name of the class
  • tag -> optional, per default it will take the name of the class

.getApp(): Server

Gets the instance of the server.

Returns the restify server instance BEFORE it started listening.

TODO

  • allow to add middleware
  • add restify server options
  • 100% test coverage