Skip to content

shirohana/dynapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dynapi

npm Travis Codecov license

A powerful WYSIWYG routes generator

Links

Features

Create a dynamic generating API server according to your directory structure, with your favorite front-end frameworks and amazing features we provided.

  • Directory structure based routing
  • Powerful flow control (using throw and next(props))
  • ESNext support
  • Customizable transform plugins
  • Complex parameter support (e.g. /flights/:from-:to and you can specify patterns of :from and :to, or custom validator)
  • Customizable middleware prefixes
  • More builders (JavaScript, TypeScript, CoffeeScript, etc.)

How it works

First, we use file watcher to track changes of files and record which file was imported from the target, then transform the files into executable Nodejs module.

Of course it's not all, we create a router according to filename and dirname, then generate middleware chains to handle different requests.

Getting started

npm install dynapi

Set up your server application like: (Install connect or express in your consider)

server/index.js
const express = require('express') // or 'connect'
const dynapi = require('dynapi')

const app = express()
app.use('/api', dynapi.factory({
  watch: process.env.NODE_ENV !== 'production',
  router: {
    src: './server',
    entry: './api',
    debug: { prefix: 'api', color: 207 }
  }
}))

app.listen(3000, () => {
  console.log('Server starts listening on localhost:3000')
})

After that, populate ./server/api/get.js inside your project:

server/api/get.js
export default (req, res) => {
  // If you're using express
  if (res.json) {
    res.json({ message: 'Hello, world!' })
  } else {
    const body = JSON.stringify({ message: 'Hello, world' })
    res.statusCode = 200
    res.setHeader('Content-Type', 'application/json')
    res.setHeader('Content-Length', Buffer.byteLength(body))
    res.end(body)
  }
}

And then start your server:

node server/index.js

Open page in browser http://localhost:3000/api

or Execute command curl -v http://localhost:3000/api

or Use requesting tools like Postman:

postman example

Examples