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

node-sparrest

v0.0.10

Published

Based on Alberto Casero's SparREST, node-sparrest brings to you a development-only sparring backend to test your APIs with.

Downloads

20

Readme

node-sparrest

Based on Alberto Casero's SparREST, node-sparrest brings to you a development-only sparring backend to test your APIs with.

Dependency Status npm version

Getting started

The way I recommend is to install the package to your local NPM project:

$ npm install node-sparrest --save-dev

And add this script within your project's package.json file:

  "scripts": {
    "sparrest": "node ./node_modules/node-sparrest/node-sparrest.js"
  }

Now you can start the node-sparrest server with the following command:

$ npm run sparrest

Custom configuration

The default configuration runs the node-sparrest server at http://127.0.0.1:8000, but you can customize it. You are allowed to override the default configuration via a local ns-config.json or ns-config.js file in your project.

To change the server's host and/or port, create a ns-config.json in your project's folder with this configuration:

{
  "server": {
    "host": "0.0.0.0",
    "port": 9000
  }
}

Also, you can change the directory where the entities are stored, being ./api the default value:

{
  "dirs": {
    "api": "./entities"
  }
}

If you want to change the default static files directory, which is ./static, try this:

{
  "dirs": {
    "static": "./uploads"
  }
}

How to

This is a quick node-sparrest walkthrough.

Create a new entity

Just do a POST request to the /api/entity path, where entity is the type of the data you want to store. Use the request body to write the JSON data to store, and don't forget to set the Content-Type header as application/json:

$ curl -X POST \
       -H "Content-Type: application/json" \
       -d '{"brand": "Gibson", "model": "Les Paul Goldtop", "year": 1957}' \
       http://localhost:8000/api/guitars

An incremental identifier will be set to the entity, starting at 1.

{
  "id": 1,
  "brand": "Gibson",
  "model": "Les Paul Goldtop",
  "year": 1957
}

Retrieve entity data

Try to do a GET request to a path pointing to its type and identifier:

$ curl http://localhost:8000/api/guitars/2

You will get the entity asked for:

{
  "id": 2,
  "brand": "PRS",
  "model": "SE Custom 22",
  "year": 2014
}

List all entities

To retrieve a collection just do a GET request using the entity type as path:

$ curl http://localhost:8000/api/guitars

Et voilà:

[{
  "id": 1,
  "brand": "Gibson",
  "model": "Les Paul Goldtop",
  "year": 1957
}, {
  "id": 2,
  "brand": "PRS",
  "model": "SE Custom 22",
  "year": 2014
}, {
  "id": 3,
  "brand": "Fender",
  "model": "Relic Telecaster Custom",
  "year": 1962
}]

Updating and patching an entity

You can override an entity by using a PUT request. You must point the request to the single entity you want to update:

$ curl -X PUT \
       -H "Content-Type: application/json" \
       -d '{"brand": "Fender"}' \
       http://localhost:8000/api/guitars/3

Your updated entity:

{
  "id": 3,
  "brand": "Fender"
}

Or you might prefer to patch it with a PATCH request:

$ curl -X PATCH \
       -H "Content-Type: application/json" \
       -d '{"model": "Relic Telecaster Custom", "year": 1962}' \
       http://localhost:8000/api/guitars/3

Your original entity is back:

{
  "id": 3,
  "brand": "Fender",
  "model": "Relic Telecaster Custom",
  "year": 1962
}

Deleting entities

To delete an entity, you must do a DELETE request to its type and identifier. Make it this way:

$ curl -X DELETE http://localhost:8000/api/guitars/3

Upload & serve a static file

Since version 0.0.8, you can upload a static file to the node-sparrest server. Just make sure you are using the /upload endpoint instead of /api/entity. The request must be a POST one:

$ curl -X POST \
       -F "file=@/Users/myuser/Desktop/relic-telecaster-custom.png" \
       http://localhost:8000/upload

A relative path to the uploaded file will be set in the response:

{
  "path": "upload/cs14cg7h4u781twf.png"
}

Then, to serve this file, just prefix the given relative path with the node-sparrest server's:

http://localhost:8000/upload/cs14cg7h4u781twf.png

More

node-sparrest is based on Alberto Casero's SparREST; this is only an attempt to port it from Python to the Node.js world. I highly recommend you to visit the SparREST repository for more info.

Status

  • The actions for GET, POST, PUT, PATCH and DELETE methods are already working.
  • It is possible to upload and serve static files.
  • The error handling is really poor at the time. I promise to put some effort in this issue. Eventually.

License

Code released under the MIT license.