dynapi v0.4.0-beta.5
Dynapi
A powerful WYSIWYG routes generator
Links
- :closed_book: Changelog
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
throwandnext(props)) - ESNext support
- Customizable transform plugins
- Complex parameter support (e.g.
/flights/:from-:toand you can specify patterns of:fromand: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 dynapiSet 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({
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.jsOpen page in browser http://localhost:3000/api
or Execute command curl -v http://localhost:3000/api
or Use requesting tools like Postman:
Examples
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago