# serve-once

> serve express middleware once

Latest version **4.0.1** (published 2026-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install serve-once
pnpm add serve-once
yarn add serve-once
bun add serve-once
```

## Health

**Score 50/100 (C)** — status: active.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2026-06-22 |
| First published | 2018-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=22.17 |
| Dependencies | 3 |
| Unpacked size | 10.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | coderaiser |
| Maintainers | coderaiser |
| Keywords | express, middleware, server, serve, once, http, get, put, post, delete |

## Links

- npm: https://www.npmjs.com/package/serve-once
- Repository: https://github.com/coderaiser/node-serve-once
- Homepage: http://github.com/coderaiser/node-serve-once
- Issues: https://github.com/coderaiser/node-serve-once/issues
- npm.io page: https://npm.io/package/serve-once

## Dependencies (3)

- [express](https://npm.io/package/express.md) ^5.2.1
- [pullout](https://npm.io/package/pullout.md) ^5.0.1
- [webpack-merge](https://npm.io/package/webpack-merge.md) ^6.0.1

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 4.0.1 (latest) — 2026-06-22
- 4.0.0 — 2026-06-22
- 3.0.2 — 2024-03-16
- 3.0.1 — 2024-03-16
- 3.0.0 — 2024-03-16
- 2.1.0 — 2021-02-03
- 2.0.1 — 2020-08-11
- 2.0.0 — 2020-02-21
- 1.7.0 — 2019-09-05
- 1.6.0 — 2018-11-14
- 1.5.0 — 2018-11-12
- 1.4.1 — 2018-11-09
- 1.4.0 — 2018-11-08
- 1.3.0 — 2018-11-08
- 1.2.3 — 2018-11-07
- … 5 more at https://npm.io/package/serve-once/versions

## README

# Serve Once [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage Status][CoverageIMGURL]][CoverageURL]

[NPMIMGURL]: https://img.shields.io/npm/v/serve-once.svg?style=flat
[BuildStatusIMGURL]: https://github.com/coderaiser/node-serve-once/actions/workflows/nodejs.yml/badge.svg
[LicenseIMGURL]: https://img.shields.io/badge/license-MIT-317BF9.svg?style=flat
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/node-serve-once/badge.svg?branch=master&service=github
[NPMURL]: https://npmjs.org/package/serve-once "npm"
[BuildStatusURL]: https://github.com/coderaiser/node-serve-once/actions/workflows/nodejs.yml "Build Status"
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"
[CoverageURL]: https://coveralls.io/github/coderaiser/node-serve-once?branch=master

Serve express middleware once.

1. creates http server on a free port
2. uses passed express middleware
3. serve exectly 1 request
4. closes http server
5. [pullout](https://github.com/coderaiser/pullout) body of request stream
6. resolves request promise with a response

Good for middleware logic testing.

## Install

```
npm i serve-once
```

## API

### request(method, path[ {options, body, type = 'string'}])

- **method** - `http`-method (`get`, `put`, `post` etc)
- **path** - `http`-path
- **options** - middleware options
- **body** - `http`-request `body`
- **headers** - `http`-request `headers`
- **type** - type of return value, can be:
  - string
  - json
  - stream
  - buffer

```js
import {serveOnce} from 'serve-once';

const middleware = (options = 'hello') => (req, res) => {
    res.end(JSON.stringify(options));
};

const {request} = serveOnce(middleware);

await request('get', '/');
// returns
'hello';

await request.get('/', {
    options: 'any',
});
// returns
'any';
```

You can send body:

```js
const pullout = require('pullout');
const putMiddleware = () => async (req, res) => {
    const body = await pullout(req);
    res.end(body);
};

import {serveOnce} from 'serve-once';

const {request} = serveOnce(putMiddleware);

const {body} = await request.put('/', {
    body: [1, 2, 3],
});

console.log(JSON.parse(body));
// returns
[1, 2, 3];
```

You can use default options:

```js
import {serveOnce} from 'serve-once';

// default options
const middleware = (options) => (req, res) => {
    res.end(JSON.stringify(options));
};

const {request} = serveOnce(middleware, {
    a: 1,
});

const options = {
    b: 2,
};

const {body} = await request.get('/', {
    options,
});

JSON.parse(body);

// returns
({
    a: 1,
    b: 2,
});
```

## License

MIT

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