# @zanix/server

> ZanixJS Framework APP Server

Latest version **2.2.0** (published 2025-03-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @zanix/server
pnpm add @zanix/server
yarn add @zanix/server
bun add @zanix/server
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2025-03-11 |
| First published | 2023-05-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 90.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ismael Calle - icalle@utp.edu.co |
| Maintainers | zanix-io, iscam2216 |

## Links

- npm: https://www.npmjs.com/package/@zanix/server
- Repository: https://github.com/zanix-io/zjs-server
- Homepage: https://github.com/zanix-io/zjs-server#readme
- Issues: https://github.com/zanix-io/zjs-server/issues
- npm.io page: https://npm.io/package/@zanix/server

## Dependencies (6)

- [@zanix/mqp](https://npm.io/package/@zanix/mqp.md) ^2.0.0
- [spdy-fixes](https://npm.io/package/spdy-fixes.md) ^4.0.5
- [@zanix/tasker](https://npm.io/package/@zanix/tasker.md) ^2.0.1
- [@zanix/datamaster](https://npm.io/package/@zanix/datamaster.md) ^2.0.0
- [@zanix/server-auth](https://npm.io/package/@zanix/server-auth.md) ^2.0.0
- [@zanix/server-core](https://npm.io/package/@zanix/server-core.md) ^2.1.0

## Recent versions

- 2.2.0 (latest) — 2025-03-11
- 1.1.0-beta.7 (beta) — 2024-06-08
- 2.1.0 — 2024-10-18
- 2.0.1 — 2024-10-16
- 2.0.0 — 2024-09-25
- 2.0.0-alpha.4 — 2024-08-21
- 2.0.0-alpha.3 — 2024-08-17
- 2.0.0-alpha.2 — 2024-08-17
- 2.0.0-alpha.1 — 2024-06-08
- 1.1.0-beta.6 — 2024-05-27
- 1.1.0-beta.5 — 2023-12-20
- 1.1.0-beta.4 — 2023-12-18
- 1.1.0-beta.3 — 2023-12-18
- 1.1.0-beta.1 — 2023-09-13
- 1.1.0-beta.0 — 2023-07-31
- … 4 more at https://npm.io/package/@zanix/server/versions

## README

# Zanix Server Library for Node.js

[![npm version](https://badge.fury.io/js/%40zanix%2Fserver.svg)](https://badge.fury.io/js/%40zanix%2Fserver) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) [![npm downloads](https://img.shields.io/npm/dt/%40zanix%2Fserver.svg)](https://www.npmjs.com/package/@zanix/server)

[![PayPal](https://img.shields.io/badge/Donate-PayPal-blue.svg)](https://paypal.me/ismaelcallem?country.x=CO&locale.x=es_XC)

## Table of Contents

- [Introduction](#introduction)
- [Installation](#installation)
- [Configuration](#configuration)
- [Getting Started](#getting-started)
- [Support and Contributions](#support-and-contributions)
- [License](#license)
- [Authors](#authors)

## Introduction

The Zanix Server Library is a powerful tool designed to simplify the process of running apps and microservices in Node.js. It provides a command-line interface (CLI) called Zanix Framework, which allows you to generate new APIs using customizable templates. These templates leverage the full range of features provided by the Zanix Server Library, enabling you to quickly build robust and scalable applications.

## Installation

To use the Zanix Server Library, you have two installation options:

### Option 1: Install Only the Library

To install only the Zanix Server Library without using a base template, follow these steps:

1. Open your project's root directory in the terminal.
2. Run the following command to install the library:

```bash
npm install @zanix/server
```

### Option 2: Install the Library template and Zanix Framework CLI

If you want to use a base template with the server library, you'll need to install the [Zanix Framework CLI](https://www.npmjs.com/package/@zanix/cli) globally on your system. Follow these steps:

1. Open your terminal.
2. Execute the following command (use `sudo` if necessary) to install the Zanix Framework CLI:

```bash
npm install -g @zanix/cli
```

Once you have installed the Zanix Framework CLI, you can proceed to generate a new API project.

### Generating a New API (with Option 2)

After installing the Zanix Framework CLI, you can easily generate a new API project. Follow these steps:

1. Open your terminal.
2. Navigate to the desired location where you want to create the API project.
3. Run the following command:

```bash
zjs new api [path] [options]
```

Replace `[path]` with the desired project path. You can also specify additional options, such as the API name and description, during the generation process.

After generating the API, navigate to the project directory and start developing your application or microservice using the Zanix Server Library. The generated project will have a pre-configured setup that maximizes the potential of the library and facilitates efficient development. For more information, review [Zanix Templates](https://github.com/zanix-io/zjs-templates#readme).

By choosing either Option 1 or Option 2, you can install the Zanix Server Library and start building powerful APIs with ease.

## Configuration

The Zanix Server Library provides a configuration file called `zanix.ts` in the `src/config` directory of your project (if you choose installation Option 2). This file allows you to customize various aspects of your API setup. Below are the available configuration options:

| Option   | Description                                                                                                 |
| -------- | ----------------------------------------------------------------------------------------------------------- |
| `PORT`   | Optional definition of PORT if not set in environment variables                                             |
| `global` | Define global settings for your API, such as Cross-Origin Resource Sharing (CORS) options.                  |
| `config` | Provide configuration options for your API, including environment variables, custom variables, and secrets. |

### Global Configuration

The `global` object in the configuration file allows you to define global settings for your API. Currently, it supports the following options:

| Option  | Description                                                                                                                                                                                                |
| ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `cors`  | Cross-Origin Resource Sharing (CORS) options. Define allowed origins, methods, and headers.                                                                                                                |
| `pipes` | Global middleware functions that are executed for every API call, allowing for validation, etc. You can also add `useGlobalPipe` at the beginning of your pipe file to avoid assigning it in this section. |

### API Configuration

The `config` object in the configuration file provides additional configuration options for your API:

| Option      | Description                                                                            |
| ----------- | -------------------------------------------------------------------------------------- |
| `envars`    | Define environment variables specific to different environments.                       |
| `variables` | Set custom variables for your API, which can be used globally across all environments. |
| `secrets`   | Specify an array of secret names that your API requires.                               |

#### Setting Environment Variables

To define environment variables for your API, use the `envars` property in the `config` object. This property allows you to specify environment-specific variables based on the defined environment names in your application.

For example, consider the following configuration:

```typescript
config: {
  envars: {
    development: {
      VAR1: 'Value 1',
      VAR2: 'Value 2'
    },
    production: {
      VAR3: 'Value 3',
      VAR4: 'Value 4'
    }
  }
}
```

In this configuration, the `envars` property defines environment variables for the `development` and `production` environments. You can add or modify these variables based on your specific requirements.

#### Setting Custom Variables

The `variables` property in the `config` object allows you to define custom variables that can be used globally across all environments. You can set these variables using a combination of key-value pairs and references to environment variables.

Here's an example:

```typescript
config: {
  variables: [
    { VAR1: 'Variable 1' },
    'VAR2=Variable 2',
    { VAR3: 'Variable 3 => ${VAR1}' }
  ]
}
```

In this configuration, three custom variables (`VAR1`, `VAR2`, and `VAR3`) are defined. The values can be assigned directly or referenced from other variables or environment variables.

#### Setting Secrets

To specify secrets for your API, use the `secrets` property in the `config` object. This property expects an array of secret names that your API requires.

For example:

```typescript
config: {
  secrets: ['secret1', 'secret2']
}
```

In this configuration, the array contains the names of the secrets required by your API. The actual secret values should be stored securely and can be accessed at runtime.

Please refer to the `zanix.ts` file for more details on how to customize your API's configuration.

### Default Environments

The Zanix Server Library comes with the following default environments:

- PORT: 8080

To start the server, set the `NODE_ENV` environment variable to one of the above values.

## Getting Started

To start your server, you can run the `zjs start [options]` command (if you choose installation Option 2) or add the following lines of code to your project file (in the other case):

```typescript
import ZanixServer from '@zanix/server'

ZanixServer.appSetup = {
  general: {
    ssr: {
      /*...*/
    },
    api: {
      /*...*/
    }
    global: {
      pipes: []
    }
  },
  config: {
    /*...*/
  }
}

ZanixServer.start(/*you can also specify the PORT here*/)
```

This will initialize and start your server using the specified configuration.

We hope you enjoy using the Zanix Server Library and find it a valuable tool for building your Node.js applications and microservices!

## Support and Contributions

To ensure compatibility between this library and the Zanix Framework CLI, please refer to the [changelog](/CHANGELOG.md#changelog) section for detailed information on versioning and compatibility. It is recommended to review the changelog to understand any specific requirements or changes associated with different versions of the library and the CLI.

If you have any questions, suggestions, or feedback, you can reach out to the author via email at [icalle@utp.edu.co](icalle@utp.edu.co). You can also connect with the author on [Linkedin](https://mx.linkedin.com/in/ismael-calle-marulanda) for updates and announcements about software.

### Next Steps

- Consult the comprehensive documentation of the Zanix Server Library (currently under construction) to gain a deeper understanding of its extensive features and learn how to take advantage of them effectively.
- Explore the project structure of the generated API, familiarizing yourself with the various files and directories that empower your development process.
- Customize and extend your API as per your unique requirements, utilizing the full spectrum of capabilities provided by the Zanix Server Library.

## License

The Zanix Server Library is an open-source project released under the [MIT License](https://opensource.org/licenses/MIT). You are free to use, modify, and distribute it as per the terms of the license.

<a name="authors"></a>

## Authors ✨

- Ismael Calle Marulanda [@iscam2216](https://github.com/iscam2216)

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