# @universal-adapter/server

> Universal Server Adapter Interface

Latest version **0.8.0** (published 2020-12-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @universal-adapter/server
pnpm add @universal-adapter/server
yarn add @universal-adapter/server
bun add @universal-adapter/server
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.8.0 |
| Published | 2020-12-19 |
| First published | 2018-12-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | brillout |

## Links

- npm: https://www.npmjs.com/package/@universal-adapter/server
- Repository: https://github.com/brillout/universal-adapter
- npm.io page: https://npm.io/package/@universal-adapter/server

## Dependencies (2)

- [@brillout/reassert](https://npm.io/package/@brillout/reassert.md) ^0.1.1
- [@brillout/url-props](https://npm.io/package/@brillout/url-props.md) ^0.1.1

## Recent versions

- 0.8.0 (latest) — 2020-12-19
- 0.7.3 — 2020-11-25
- 0.7.2 — 2020-11-25
- 0.7.1 — 2020-11-25
- 0.7.0 — 2020-11-24
- 0.6.0 — 2020-10-06
- 0.5.1 — 2019-12-12
- 0.5.0 — 2019-12-10
- 0.4.2 — 2019-10-14
- 0.4.1 — 2019-10-03
- 0.4.0 — 2019-07-31
- 0.3.0 — 2019-06-27
- 0.2.1 — 2019-05-27
- 0.2.0 — 2019-05-27
- 0.1.2 — 2018-12-22
- … 2 more at https://npm.io/package/@universal-adapter/server/versions

## README

<!---






    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.






-->

# `@universal-adapter`

Like adapters for power outlet, but for JavaScript.

For now, there are only adapters for server frameworks (Express, Hapi, Koa).

## Server adapters

The server adapters allow you to write server code that can work with several server frameworks.

 - Express adapter: `@universal-adapter/express`
 - Hapi adapter: `@universal-adapter/hapi`
 - Koa adapter: `@universal-adapter/koa`

### Example

We define routes `/` and `/hello/{name}` that will work with Express, Hapi, and Koa:

~~~js
// /example/helloPlug.js

module.exports = helloPlug;

async function helloPlug({pathname, method}) {
  if( method!=='GET' || !pathname.startsWith('/hello/') ) {
    return null;
  }
  return {
    body: 'hello '+pathname.slice('/hello/'.length),
    headers: [
      {name: 'Cache-Control', value: 'public, max-age=31536000, immutable'}
    ]
  };
}
~~~

We can now use `helloPlug` with either Express, Hapi, or Koa:

~~~js
// /example/express

const express = require('express');
const ExpressAdater = require('@universal-adapter/express');
const helloPlug = require('../helloPlug');

module.exports = start();

function start() {
  const app = express();

  app.use(
    new ExpressAdater([
      helloPlug,
    ])
  );

  app.listen(3000, () => console.log('Express server running at http://localhost:3000'));
}
~~~
~~~js
// /example/hapi

const Hapi = require('hapi');
const HapiAdapter = require('@universal-adapter/hapi');
const helloPlug = require('../helloPlug');

module.exports = start();

async function start() {
  const server = Hapi.Server({
    port: 3000,
    debug: {request: ['internal']},
  });

  await server.register(
    new HapiAdapter([
      helloPlug,
    ])
  );

  await server.start();
  console.log('Hapi server running at http://localhost:3000');
}
~~~
~~~js
// /example/koa

const Koa = require('koa');
const KoaAdapter = require('@universal-adapter/koa');
const helloPlug = require('../helloPlug');

module.exports = start();

function start() {
  const server = new Koa();

  server.use(
    new KoaAdapter([
      helloPlug,
    ])
  );

  server.listen(3000);

  console.log('Koa server running at http://localhost:3000');
}
~~~

<!---






    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.












    WARNING, READ THIS.
    This is a computed file. Do not edit.
    Edit `/readme.template.md` instead.






-->

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