npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fixed-server

v0.4.0

Published

Server for HTTP fixtures

Downloads

45

Readme

fixed-server Build status

Server for HTTP fixtures

This was built to make common responses from an API consistent across tests.

For convenience, there are methods which setup/teardown a server for mocha (see factory.run).

Getting Started

Install the module with: npm install fixed-server

// Create a server that we can setup/teardown during tests
var assert = require('assert');
var request = require('request');
var FixedServer = require('fixed-server');
var fixedServer = new FixedServer({port: 1337});

// Create a fixture that we can refer to during the test
// DEV: `method` and `route` are done via `express` which allows for complex methods/routing
fixedServer.addFixture('GET 200 /hello', {
  method: 'get',
  route: '/hello',
  response: function (req, res) {
    res.send('world');
  }
});

// Start our test
describe('A server', function () {
  // Automatically setup/teardown server with our fixture
  fixedServer.run('GET 200 /hello');

  it('responding to a request', function (done) {
    request('http://localhost:1337/hello', function (err, res, body) {
      assert.strictEqual(body, 'world');
      done();
    });
  });
});

Documentation

fixed-server exposes FixedServerFactory via its module.exports. Under the hood, FixedServer runs via express.

new FixedServerFactory(options)

Constructor for creating new FixedServer's

  • options Object - Container for options
    • port Number - Port to run created FixedServer's from

FixedServerFactory.fromFile(filepath, options)

Helper to quickly generate a server with fixtures from a file

  • filepath String - Path to fixtures to load in
    • This will be loaded via require and passed in to factory.addFixtures
  • option Object - Options to pass to FixedServerFactory constructor

factory.addFixture(name, params)

Add a new fixture to the list of potential fixture to load into child servers.

  • name String - Key to store fixture under
  • params Object - Container for fixture info
    • method String - Lowercase HTTP method to run params.response under (e.g. get, post, put)
    • route String|RegExp - Route to run params.response under (e.g. /hello)
    • response Function - express middleware that will handle request and generate response
      • Function signature must be (req, res) as is expected in express

factory.addFixtures(obj)

Add multiple fixtures to our list of fixtures

  • obj Object - Container for multiple fixtures
    • Each key-value pair will be used as name and params respectively for FixedServer.addFixtures

factory.createServer(fixtureNames)

Create a FixedServer with fixtureNames running on it

  • fixtureNames String|String[] - Single fixture name or array of fixture names to load into server
    • Each of these will be loaded via server.installFixture

factory.run(fixtureNames)

Helper method for running server inside of mocha tests

  • fixtureNames String|String[] - Information to pass onto factory.createServer

FixedServer(options)

Create a server to host fixtures on

  • options Object - Container for options
    • port Number - Port to run server from via .listen()

server.listen(port)

Start listening for requests

  • port Number - Port to start listening against. If not provided, it will attempt to use options.port.

server.destroy(cb)

Tear down the server

  • cb Function - Optional error-first callback to run when the server teardown is completed

server.installFixture(fixture)

Add a new route to the server

  • fixture Object - Container for route parameters
    • method String - Lowercase HTTP method to run params.response under (e.g. get, post, put)
    • route String|RegExp - Route to run fixture.response under (e.g. /hello)
    • response Function - express middleware that will handle request and generate response
      • Function signature must be (req, res) as is expected in express
server.installFixture({
  method: 'get',
  route: '/hello',
  response: function (req, res) {
    res.send('world');
  }
});
// converts to
express().get('/hello', function (req, res) {
  res.send('world');
});

server.run()

Helper method for running the server inside of mocha tests

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test.

License

Copyright (c) 2014 Uber

Licensed under the MIT license.