# jovy

> Express-based declarative server framework inspired by Nest

Latest version **1.2.10** (published 2023-06-12) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install jovy
pnpm add jovy
yarn add jovy
bun add jovy
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.10 |
| Published | 2023-06-12 |
| First published | 2022-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 46.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (Unknown) |
| Author | Evgenii Scherbakov |
| Maintainers | iipekolict-org |
| Keywords | framework, server, express, declarative |

## Links

- npm: https://www.npmjs.com/package/jovy
- Repository: https://github.com/evgenii-shcherbakov/jovy
- Homepage: https://github.com/evgenii-shcherbakov/jovy#readme
- Issues: https://github.com/evgenii-shcherbakov/jovy/issues
- npm.io page: https://npm.io/package/jovy

## Dependencies (5)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [dotenv](https://npm.io/package/dotenv.md) ^16.0.1
- [express](https://npm.io/package/express.md) ^4.18.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [express-fileupload](https://npm.io/package/express-fileupload.md) ^1.4.0

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads
- [@modern-js/plugin-swc](https://npm.io/package/@modern-js/plugin-swc.md) — 3.4K weekly downloads

## Recent versions

- 1.2.10 (latest) — 2023-06-12
- 1.2.9 — 2023-06-12
- 1.2.8 — 2022-07-03
- 1.2.7 — 2022-07-03
- 1.2.6 — 2022-07-03
- 1.2.5 — 2022-07-03
- 1.2.4 — 2022-06-26
- 1.2.3 — 2022-06-26
- 1.2.2 — 2022-06-26
- 1.2.1 — 2022-06-26
- 1.1.2 — 2022-06-22
- 1.1.1 — 2022-06-22
- 1.0.8 — 2022-06-12
- 1.0.7 — 2022-06-12
- 1.0.6 — 2022-06-12
- … 5 more at https://npm.io/package/jovy/versions

## README

# jovy
Express-based declarative server framework inspired by Nest

[![npm version](https://img.shields.io/npm/v/jovy.svg)](https://npmjs.org/package/jovy)
[![npm license](https://img.shields.io/npm/l/jovy.svg)](https://npmjs.org/package/jovy)
[![npm type definitions](https://img.shields.io/npm/types/jovy)](https://npmjs.org/package/jovy)

```typescript
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](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/).

Before installing, [download and install Node.js](https://nodejs.org/en/download/).
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](https://docs.npmjs.com/creating-a-package-json-file).

Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```shell
npm install jovy reflect-metadata
```

If needed, you can also add express deps:

```shell
npm install express
npm install -D @types/express
```

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

```typescript
import 'reflect-metadata';
```

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

### Documentation

##### Middlewares

Example of using middlewares:

```typescript
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:

```typescript
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:

```typescript
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:

```typescript
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
};
```

---
_Source: https://npm.io/package/jovy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
