Package Exports
- serve-once
- serve-once/lib/serve-once.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (serve-once) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Serve Once

Serve express middleware once.
- creates http server on a free port
- uses passed express middleware
- serve exectly 1 request
- closes http server
- pullout body of request stream
- 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
-requestbody
- headers -
http
-requestheaders
- type - type of return value, can be:
- string
- json
- stream
- buffer
const middleware = (options = 'hello') => (req, res) => {
res.end(JSON.stringify(options));
};
const {request} = require('serve-once')(middleware);
await request('get', '/');
// returns
'hello';
await request.get('/', {
options: 'any',
});
// returns
'any';
You can send body:
const pullout = require('pullout');
const putMiddleware = () => async (req, res) => {
const body = await pullout(req);
res.end(body);
};
const {request} = require('serve-once')(putMiddleware);
const {body} = await request.put('/', {
body: [1, 2, 3],
});
console.log(JSON.parse(body));
// returns
[1, 2, 3];
You can use default options:
// default options
const middleware = (options) => (req, res) => {
res.end(JSON.stringify(options));
};
const {request} = require('serve-once')(middleware, {
a: 1,
});
const options = {
b: 2,
};
const {body} = await request.get('/', {
options,
});
JSON.parse(body);
// returns
({
a: 1,
b: 2,
});
License
MIT