# @dash4/server

> dash4 server

Latest version **0.9.3** (published 2020-12-07) · MIT license · 0 weekly downloads

## Install

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

Provides the command `dash4`.

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.9.3 |
| Published | 2020-12-07 |
| First published | 2019-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 30.8 KB |
| Known vulnerabilities | 0 (+4 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 20 |
| Author | Simon Mollweide |
| Maintainers | smollweide |

## Links

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

## Dependencies (8)

- [ws](https://npm.io/package/ws.md) 7.4.1
- [open](https://npm.io/package/open.md) 7.3.0
- [uuid](https://npm.io/package/uuid.md) 8.3.1
- [tslib](https://npm.io/package/tslib.md) 2.0.3
- [commander](https://npm.io/package/commander.md) 6.2.0
- [@dash4/log](https://npm.io/package/@dash4/log.md) 0.9.3
- [mime-types](https://npm.io/package/mime-types.md) 2.1.27
- [@dash4/client](https://npm.io/package/@dash4/client.md) 0.9.3

## Recent versions

- 0.9.3 (latest) — 2020-12-07
- 0.9.2 — 2020-11-18
- 0.9.1 — 2020-11-18
- 0.9.0 — 2020-08-22
- 0.8.5 — 2020-06-05
- 0.8.4 — 2020-05-09
- 0.8.3 — 2020-05-01
- 0.8.2 — 2020-04-09
- 0.8.1 — 2020-03-15
- 0.8.0 — 2020-01-24
- 0.7.1 — 2019-12-20
- 0.7.0 — 2019-11-15
- 0.6.0 — 2019-11-03
- 0.5.2 — 2019-10-24
- 0.5.1 — 2019-07-20
- … 6 more at https://npm.io/package/@dash4/server/versions

## README

<div align="center">
<h1>Dash4 Server</h1>
Dash4 webapp server application
<br />
<br />

[![NPM version](https://badge.fury.io/js/%40dash4%2Fserver.svg)](https://www.npmjs.com/package/@dash4/server)
[![License](https://img.shields.io/badge/license-MIT-green.svg)](http://opensource.org/licenses/MIT) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![Prettier](https://img.shields.io/badge/Code%20Style-Prettier-green.svg)](https://github.com/prettier/prettier) ![TypeScript-ready](https://img.shields.io/npm/types/@dash4/server.svg)

<br />
</div>

The following documentation is just relevant for dash4 plugin development. For informations on how to use Dash4 please read this: [Dash4 documentation](https://github.com/smollweide/dash4/blob/master/README.md)

## Table of Contents

* [Installation](#installation)
* [Usage](#usage)
* [License](#license)

## <a name="installation">Installation</a>

```shell
npm i @dash4/server
```

## <a name="usage">Usage</a>

```ts
import { Dash4Plugin, IDash4Plugin } from '@dash4/server';
import { IncomingMessage, ServerResponse } from 'http';

interface IClientConfig {
  something: string;
}

interface IOptions {
  something: string;
  // enable/disable dark mode
  dark?: boolean;
  // grid with per breakpoint
  // [12,6,3] means 100% width on small viewports, 50% on medium viewports and 33.3% on large viewports
  width?: number[];
}

class PluginName extends Dash4Plugin implements IDash4Plugin<IClientConfig> {
  
  private _something: string;

  constructor({ dark, width, something }: IOptions) {
    super({
      dark,
      width,
      name: 'PluginName',
      lowerCaseName: 'plugin-name',
    });

    this._something = something;
  }

  /**
   * clientConfig object will be accessible in client via Plugin component props 
   */
  public get clientConfig() {
    return {
      something: this._something,
    };
  }

  /**
   * add files as script/style tag to the index.html and make them available
   * it's also possible to define them like this:
   * {
   *   pathName: string;
     *   scriptTag?: boolean;
   * }
   * this is needed for dynamic imports
   */
  public get clientFiles() {
    return [path.join(__dirname, '../../dist/plugins/plugin-name/main.js')];
  }

  /**
   * method connected will be executed after a connection via sockets is established
   * super class provides a `on` and `send` method
   */
  public connected = async (on: TOn, send: TSend) => {
    this.on('connected', () => {
      this.send('data', 'some conntent');
    });
  };

  /**
   * method severRequest allowes to provide rest endpoints (for example images)
   */
  public serverRequest = async (req: IncomingMessage, res: ServerResponse) => {
    if (req.url === imagePublicPath) {
      res.writeHead(200, {
        'Content-Type': 'image/png',
      });
      res.end(await fs.readFile(imageDiskPath));
      return true;
    }
    return false;
  };
}
```

## License

The @dash4/server is [MIT licensed](./LICENSE)

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