# @dmail/server

> [![npm package](https://img.shields.io/npm/v/@dmail/server.svg)](https://www.npmjs.com/package/@dmail/server) [![build](https://travis-ci.com/dmail/server.svg?branch=master)](http://travis-ci.com/dmail/server) [![codecov](https://codecov.io/gh/dmail/serve

Latest version **2.10.0** (published 2019-09-27) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.10.0 |
| Published | 2019-09-27 |
| First published | 2019-06-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 7 |
| Unpacked size | 471.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | dmail |

## Links

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

## Dependencies (7)

- [kill-port](https://npm.io/package/kill-port.md) 1.5.2
- [@jsenv/href](https://npm.io/package/@jsenv/href.md) 1.0.0
- [@dmail/helper](https://npm.io/package/@dmail/helper.md) 2.3.0
- [@jsenv/logger](https://npm.io/package/@jsenv/logger.md) 1.0.0
- [@dmail/cancellation](https://npm.io/package/@dmail/cancellation.md) 2.6.0
- [@dmail/process-signals](https://npm.io/package/@dmail/process-signals.md) 1.1.0
- [@jsenv/operating-system-path](https://npm.io/package/@jsenv/operating-system-path.md) 2.5.0

## Recent versions

- 2.10.0 (latest) — 2019-09-27
- 2.9.0 — 2019-09-02
- 2.8.0 — 2019-08-29
- 2.7.0 — 2019-08-23
- 2.6.0 — 2019-08-22
- 2.5.0 — 2019-08-19
- 2.4.0 — 2019-08-19
- 2.3.0 — 2019-08-16
- 2.2.0 — 2019-08-16
- 2.1.0 — 2019-08-15
- 2.0.0 — 2019-08-13
- 1.15.0 — 2019-08-13
- 1.14.0 — 2019-08-13
- 1.13.0 — 2019-07-17
- 1.12.0 — 2019-07-17
- … 11 more at https://npm.io/package/@dmail/server/versions

## README

# Server

[![npm package](https://img.shields.io/npm/v/@dmail/server.svg)](https://www.npmjs.com/package/@dmail/server)
[![build](https://travis-ci.com/dmail/server.svg?branch=master)](http://travis-ci.com/dmail/server)
[![codecov](https://codecov.io/gh/dmail/server/branch/master/graph/badge.svg)](https://codecov.io/gh/dmail/server)

> Simplified api to create server using node.js.

## Introduction

`@dmail/server` is used to start a node.js server.<br />

The main exports are:

- `startServer`
- `firstService`
- `serveFile`

## `startServer` Example

The following code starts a server listening to `http://127.0.0.1:8080` responding `Hello world` as plain text.

```js
import { startServer } from "@dmail/server"

startServer({
  protocol: "http",
  ip: "127.0.0.1",
  port: 8080,
  requestToResponse: () => {
    return {
      status: 200,
      headers: {
        "content-type": "text/plain",
      },
      body: "Hello world",
    }
  },
})
```

If you want to know more about `startServer`, there is a dedicated page documenting it.<br />
— see [`startServer` documentation](./docs/start-server-doc.md)

## `serveFile` example

The following code starts a server listening to `http://127.0.0.1:8080` serving files of the current directory.

```js
import { startServer, serveFile } from "@dmail/server"

startServer({
  protocol: "http",
  ip: "127.0.0.1",
  port: 8080,
  requestToResponse: ({ ressource, method, headers }) =>
    serveFile(`${__dirname}${ressource}`, {
      method,
      headers,
    }),
})
```

If you want to know more about `serveFile`, there is a dedicated page documenting it.<br />
— see [`serveFile` documentation](./docs/serve-file-doc.md)

## `firstService` example

Let's make a `generateResponse` function with the following behaviour:

- returns `204 no content` response for `/`
- returns `200 ok` response for `/whatever`

```js
import { firstService } from "@dmail/server"

const generateResponse = (request) => {
  return firstService(
    () => {
      if (ressource !== "/") return null
      return { status: 204 }
    },
    () => {
      if (ressource !== "/whatever") return null
      return { status: 200 }
    },
  )
}
```

If you want to know more about `firstService`, there is a dedicated page documenting it.<br />
— see [`firstService` documentation](./docs/first-service-doc.md)

## Installation

```console
npm install @dmail/server@2.4.0
```

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