Socket
Socket
Sign inDemoInstall

circle

Package Overview
Dependencies
8
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    circle

Minimalistic JSON API Server


Version published
Weekly downloads
27
decreased by-34.15%
Maintainers
1
Install size
103 kB
Created
Weekly downloads
 

Readme

Source

circle

Minimalistic NodeJS API Server.

Install

$ npm install circle

Usage

Defining A Simple Server

circle = require('circle')

api = circle({
  '/person/:name/:surname': person,
  '/company/:id': company,
  '/': home
})

api.start(8080, 'localhost')

Routing

Accepting requests to URLs like /person/john/smith?email=john@smith.com

function person (reply, match) {
  reply(undefined, { name: match.params.name, surname: match.params.surname, email: match.params.query.email })
}

This will output:

{
        "ok": true,
        "result": [
                {
                        "name": "john",
                        "surname": "smith",
                        "email": "john@smith.com"
                }
        ]
}

Producing errors:

function company (reply, match) {
  reply({ not_implemented: true }) // returns error
}

It handles POST data and file uploads nicely:

function create (reply, match, post, files) {
  match.params
  // => { name: 'foo', age: 21 }

  files
  // => { profilePicture: { path: '/tmp/foo.tar.gz', size: 1024 }}

  reply (undefined, { created: true })
}

JSONP

Circle outputs the response in JSONP format for requests made by passing "callback" parameter.

Formatting Output For Specific "Accept" Types

Circle servers will output JSON by default. To format the output for specific "Accept" header:

api.format('/person/:name/:surname', 'text/plain', function (response) {
  if (response.error) return 'Error: ' + response.error;

  return 'Name: ' + response.result.name + ' Surname: ' + response.result.surname + ' E-Mail: ' + response.result.email;
});

Will output below for curl http://localhost:8080/person/john/smith?email=john@smith.com:

Name: John
Surname: Smith
Email: john@smith.com

Keywords

FAQs

Last updated on 16 Dec 2013

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc