Giter VIP home page Giter VIP logo

node-serve-once's Introduction

Serve Once License NPM version Dependency Status Build Status Coverage Status

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 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
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

node-serve-once's People

Contributors

coderaiser avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.