GithubHelp home page GithubHelp logo

circle's Introduction

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/[email protected]

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": "[email protected]"
                }
        ]
}

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/[email protected]:

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

circle's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.