A powerful WYSIWYG routes generator
- 📕 Changelog
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.)
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.
npm install dynapi
Set up your server application like: (Install connect or express in your consider)
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:
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:
