Skip to content

[UNLOVED] Minimal routing layer for building JSON APIs with Zeit's Micro

Notifications You must be signed in to change notification settings

possibilities/micro-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

87 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Micro API

THIS PROJECT IS NOT MAINTAINED. PLEASE USE https://github.com/pedronauck/micro-router

Summary

Minimal routing layer for building JSON APIs with Zeit's Micro

CircleCI

Installation

yarn add micro micro-api

Usage

Declare your API's routes

// index.js
const microApi = require('micro-api')
const handlers = require('./handlers')

const api = microApi([
  {
    method: 'post',
    path: '/foos',
    handler: handlers.createFoo,
  },
  {
    method: 'get',
    path: '/foos/:fooId',
    handler: handlers.showFoo,
  },
])

module.exports = api

Define some micro-compatible handlers

// handlers.js
const uuid = require('uuid')

// In memory database
const foos = []

const createFoo = ({ body }) => {
  // Build up the new item
  const newFoo = { ...body, id: uuid() }
  // Add it to the database
  foos.push(newFoo)
  return newFoo
}

// Find and return by id
const showFoo = ({ params: { fooId } }) => foos.find(f => f.id === fooId)

module.exports = { createFoo, showFoo }

Run

micro -p 3000 ./index.js

See Micro documentation for complete usage.

About

[UNLOVED] Minimal routing layer for building JSON APIs with Zeit's Micro

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •