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

jovy

v1.2.10

Published

Express-based declarative server framework inspired by Nest

Downloads

6

Readme

jovy

Express-based declarative server framework inspired by Nest

npm version npm license npm type definitions

import { Controller, Get, Param, HandlerType } from 'jovy';

@Controller()
class MainController {
  @Get()
  getHello(): string {
    return 'Hello';
  }

  @Get(':id/posts/:postId', HandlerType.RENDER)
  getId(@Param('id') id: string, @Param('postId') postId: string): string {
    return `
      <div>
        <h2>Id: ${id}</h2>
        <p>PostId: ${postId}</p>
      </div>
    `;
  }
}

const config: AppConfiguration = {
  port: 3000,
  controllers: [MainController],
};

new App(config).launch();

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js. Node.js 16 or higher is required.

If this is a brand new project, make sure to create a package.json first with the npm init command.

Installation is done using the npm install command:

npm install jovy reflect-metadata

If needed, you can also add express deps:

npm install express
npm install -D @types/express

Add string below in the top of main app file (main.ts, for example)

import 'reflect-metadata';

Add flags "experimentalDecorators": true and "emitDecoratorMetadata": true in tsconfig.json file

Documentation

Middlewares

Example of using middlewares:

import { setMiddlewares, Middleware, Controller, Get, Request, Response, NextFunction } from 'jovy';

// create you middleware, like in express
export const authMiddleware: Middleware = async (
  req: Request,
  res: Response,
  next: NextFunction
) => {
  res.json({ id: req.params.id, message: 'middleware worked' });
};

// create decorator, using function setMiddlewares from jovy
export const Auth = (): MethodDecorator => setMiddlewares(authMiddleware);

// add decorator to needed functions in controllers
@Controller()
export default class MiddlewareController {
  @Get('middleware')
  @Auth() // our decorator!
  getMiddleware() {
    return { message: 'middleware' };
  }
}
Errors

Example of using error handling:

import {
  App,
  AppConfiguration,
  Controller,
  CustomError,
  ErrorHandler,
  Get,
  NextFunction,
  Request,
  Response,
} from 'jovy';

// local error handler, can be used with CustomError decorator for only selected functions
const customError: ErrorHandler = async (
  err: Error,
  req: Request,
  res: Response,
  next: NextFunction
) => {
  res.json({ error: err.message });
};

// global error handler, will be used, if no any local handlers present
const customGlobalErrorHandler: ErrorHandler = async (
  err: Error,
  req: Request,
  res: Response,
  next: NextFunction
) => {
  res.json({ error: true });
};

@Controller('error')
export default class ErrorController {
  @Get()
  getError() {
    throw new Error(); // triggers global handler
  }

  @Get('custom')
  @CustomError(customError) // decorator for adding local error handlers
  getCustomError() {
    throw new Error(); // triggers local handler
  }
}

const config: AppConfiguration = {
  port: 3000,
  controllers: [ErrorController],
  errorHandler: customGlobalErrorHandler, // connect your global error handler in app config
};

new App(config).launch();
Launch

Examples of launch callback customizations:

import { App, Application } from 'jovy';

// sync example
new App().launch((app: Application, port: string | number) => {
  app.listen(port, () => console.log(port));
});

// async example
new App().launch(async (app: Application, port: string | number) => {
  const delayedPort: string | number = await new Promise((res) =>
    setTimeout(() => res(port), 1000)
  );

  app.listen(port, () => console.log(delayedPort));
});
Configuration

About app config object:

import { Application, NextFunction, Request, Response, Handler } from 'express';

interface IController {
  [handleName: string]: Handler;
}

type ControllerClass = new (...args: any) => IController;
type ConfigureAppFunc = (app: Application) => void;

type ErrorHandler = (
  err: Error,
  req: Request,
  res: Response,
  next: NextFunction
) => Promise<void> | void;

type AppConfiguration = {
  port?: number | string; // used by server port
  disableCors?: boolean; // add true, if need disable cors
  routesInfo?: boolean; // set false, if no need show in console routes table
  controllers?: ControllerClass[]; // controlller classes arr
  middlewares?: any[]; // express middlewares (global)
  configure?: ConfigureAppFunc; // function for more deeper customize app, if needed
  errorHandler?: ErrorHandler; // global error handler
};