Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
fake-api-server
Advanced tools
a simple, easy mock REST API server
// 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
...
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"
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
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.
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)
pluralName
).idAttribute([newVal])
id
.idFactory([newVal])
name([newVal])
pluralName([newVal])
all()
add(record)
find(id)
update
and remove
return false
if the id is
not found for this resource.update(id, updates)
remove(id)
###Server###
new Server()
listen([port=3000])
register(resource)
use(middleware)
server.use(express.static(dirname))
.static(path)
Contributions are most welcome! Please maintain the style of the existing code and docs, which includes:
Many thanks to the following folks for their contributions!
FAQs
A simple, easy mock REST API server.
We found that fake-api-server demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.