Skip to content
a simple, easy mock REST API server
CoffeeScript JavaScript
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.


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")
    name: "Lords of Finance",
    author: "Liaquat Ahamed",
    year: 2009 })
    name: "Public Enemies",
    author: "Bryan Burrough",
    year: 2004

var server = new fake.Server()
> npm install fake-api-server
npm http GET
npm http 200
fake-api-server@0.4.0 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"

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

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

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

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

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

see example and test/integration for more examples


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.


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


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


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


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


  • gets or sets the name of the resource.


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


  • get all records for this resource.


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


  • 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 a record by id, returning a boolean indicating success.


new Server()

  • creates a new server.


  • starts listening for REST requests on the given port.


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


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


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


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
  • @IgorGanapolsky
  • @krcourville
You can’t perform that action at this time.