# serverlet

> Use ordinary functions to serve HTTP requests

Latest version **0.1.3** (published 2024-04-03) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.3 |
| Published | 2024-04-03 |
| First published | 2021-10-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 31.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | William Swanson |
| Maintainers | swansontec |

## Links

- npm: https://www.npmjs.com/package/serverlet
- Repository: https://github.com/swansontec/serverlet
- Homepage: https://github.com/swansontec/serverlet#readme
- Issues: https://github.com/swansontec/serverlet/issues
- npm.io page: https://npm.io/package/serverlet

## Recent versions

- 0.1.3 (latest) — 2024-04-03
- 0.1.2 — 2023-02-24
- 0.1.1 — 2021-10-14
- 0.1.0 — 2021-10-14

## README

# serverlet

[![Build Status](https://app.travis-ci.com/swansontec/serverlet.svg?branch=main)](https://app.travis-ci.com/swansontec/serverlet)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

A serverlet is a function that accepts an HTTP request and returns an HTTP response:

```js
const helloRoute = (request: HttpRequest): HttpResponse => {
  return {
    headers: { 'content-type': "text/plain" }
    body: 'Hello, world!'
  }
}
```

You can use a serverlet as an Express route:

```js
import { makeExpressRoute } from 'serverlet/express'

app.get('/', makeExpressRoute(helloRoute))
```

You can also use a serverlet to mock the `fetch` function:

```js
import { makeFetchFunction } from 'serverlet'

window.fetch = makeFetchFunction(helloRoute)

// Now run some tests...
```

Of course, just serving a single route isn't very interesting. A more complete server will combine many serverlets together:

```js
import { pickPath, pickMethod, withCors } from 'serverlet'

const server = pickPath({
  '/hello': withCors(helloRoute),
  '/api/user': pickMethod({
    GET: getUserRoute,
    POST: postUserRoute
  })
}, missingRoute)
```

The `pickPath` and `pickMethod` helper functions combine multiple smaller serverlets together into a bigger serverlet, so the returned `server` is just another serverlet.

The `withCors` helper function is an example of a middleware. It accepts a serverlet, and returns an enhanced serverlet that sets the appropriate headers.

You can easily create your own middleware functions:

```js
export const withApiKey = (route: Serverlet): Serverlet => request => {
  // Bail out if we don't have an API key:
  if (request.headers.authorization !== expectedApiKey) {
    return { status: 401 }
  }

  // Otherwise, run the route:
  return route(request)
}
```

The `Serverlet` type is generic, so middleware can add properties to the request or response objects, and it's all type-safe.

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