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

fake-api-server

v0.4.0

Published

A simple, easy mock REST API server.

Downloads

37

Readme

fake api server

a simple, easy mock REST API server

  • quick start
  • introduction
  • dependencies
  • api reference
  • contributing

Build Status

quick start

// server.js
var fake = require('fake-api-server');

var books = new fake.Resource("Book")
  .add({
    name: "Lords of Finance",
    author: "Liaquat Ahamed",
    year: 2009 })
  .add({
    name: "Public Enemies",
    author: "Bryan Burrough",
    year: 2004
  });

var server = new fake.Server()
  .register(books)
  .listen(3000);
> npm install fake-api-server
npm http GET https://registry.npmjs.org/fake-api-server/0.4.0
npm http 200 https://registry.npmjs.org/fake-api-server/0.4.0
...
[email protected] node_modules/fake-api-server

> node server.js &
[1] 1337
server listening on localhost:3000

> curl localhost:3000/api/books
[{"id":1,"name":"Lords of Finance","author":"Liaquat Ahamed
","year":2009},{"id":2,"name":"Public Enemies","author":"Br
yan Burrough","year":2004}]

> curl localhost:3000/api/books -X POST \
  -d {"name":"Low Life","author":"Luc Sante","year":1992} \
  -H "Content-Type: application/json"
OK

> curl localhost:3000/api/books/3
{"id":3,"name":"Low Life","author":"Luc Sante","year":1992}

> curl localhost:3000/api/books/3 -X PUT -d {"year":1991} \
  -H "Content-Type: application/json"
OK

> curl localhost:3000/api/books/3
{"id":3,"name":"Low Life","author":"Luc Sante","year":1991}

> curl localhost:3000/api/books/3 -X DELETE
OK

> curl localhost:3000/api/books/1 -X DELETE
OK

> curl localhost:3000/api/books
[{"id":2,"name":"Public Enemies","author":"Bryan Burrough",
"year":2004}]

see example and test/integration for more examples

introduction

you hold in your hands an extremely simple fake API server built in Node.js and aptly named fake-api-server. this little project came about after I wrote a comment on this HN thread responding to an article by Jeremy Morgan about implementing the same thing on a "self-hosted" .NET server.

since the crux of that article was that you want the fake API to get out of the way so you can focus on front-end development, it seemed prudent to rewrite the code in JavaScript to avoid context-switching between languages.

dependencies

  • node
  • express
  • body-parser

api reference

the API is made up of two types: Resource and Server. you create a number of resources which you then register with a server. the server listens for requests to /api routes and handles the various registered resources RESTfully.

any method that doesn't otherwise return a value returns the object itself to allow for method chaining.

###Resource###

new Resource(name)

  • creates a new resource with the given name, which should be in singular form. the name will be pluralized for the api (see the documentation for pluralName).

idAttribute([newVal])

  • gets or sets the id attribute name, which defaults to id.

idFactory([newVal])

  • gets or sets the new record id factory. this should be a method that returns a novel unique id each time it's called.

name([newVal])

  • gets or sets the name of the resource.

pluralName([newVal])

  • gets or sets the plural name of the resource, which defaults to the name plus "s".

all()

  • get all records for this resource.

add(record)

  • add a record to the resource data store. the id attribute will be automatically set. the record is not copied, so if you need the new id you can get it from the object reference passed in.

find(id)

  • finds a record by id.
  • this method, update and remove return false if the id is not found for this resource.

update(id, updates)

  • update a record by id, returning the updated record.

remove(id)

  • remove a record by id, returning a boolean indicating success.

###Server###

new Server()

  • creates a new server.

listen([port=3000])

  • starts listening for REST requests on the given port.

register(resource)

  • register a given resource for the API. the appropriate REST verbs will be routed.

use(middleware)

  • add a middleware to the underlying express server. for example, to serve static content, try server.use(express.static(dirname)).

static(path)

  • a convenience method for adding the express builtin static middleware, as in the above example. useful if you would not have otherwise needed to load express yourself. serve static html, javascript, images and other content along with the fake api.

contributing

Contributions are most welcome! Please maintain the style of the existing code and docs, which includes:

  • add tests and documentation for new features
  • limit line length to 70 characters
  • run Grunt to build and test
  • commit the CoffeeScript compilation seperately

Many thanks to the following folks for their contributions!

  • @mindeavor
  • @tornad
  • @temnoregg
  • @evrenkutar
╭╮☲☲☲╭╮