@streammedev/parrot-server
v1.0.3
Published
Simple http server which returns a body with information about the request such as method, pathname, query parameters, and the request body. It is useful for testing.
Readme
Parrot Server
Simple server responds to any HTTP request with details about the request.
Install
npm install @streammedev/parrot-server;Usage
First, import the class:
import ParrotServer from '@streammedev/parrot-server';Next, instantiate it:
const server = new ParrotServer();Next, start it:
server.start();Your server is now accepting requests at http://localhost.
Once you're done, stop the server by calling stop, ie:
server.stop(); API
constructor(port) - Instantiates a new server instance on the specified (optional) port. Whether it is set explicitly or not, server.port will be set to the port once the server has started.
start(cb) - Starts the server, then calls the (optional) callback.
stop(cb) - Stops the server, then calls the (optional) callback.
CLI
A simple cli command is available:
parrot-server --port=8080Example
import ParrotServer from '@streammedev/parrot-server';
import assert from 'assert';
import request from 'request';
describe('do some test', function () {
let testServer;
before(function (done) {
testServer = new ParrotServer(port);
testServer.start(done);
});
after(function (done) {
testServer.stop(done);
});
it('should get my endpoint', function(done) {
request('http://localhost/my-endpoint?q=findme', function (error, response, body) {
assert.ifError(error);
assert.ifError(response.statusCode !== 200);
assert.strictEqual(body.method, 'GET', 'it should be a GET request');
assert.strictEqual(body.path, 'my-endpoint', 'it should have the right path');
assert.strictEqual(body.query.q, 'findme', 'it should set the q query parameter');
})
})
})
}Contributing
Contributions are welcome. Please see our guidelines in CONTRIBUTING.md
