yion v2.1.1
YION
The most yionly (lighter, maybe ...) node framework.
Installation
$ npm install --save yion
Usage
Get started with Yion
const { createApp, createServer } = require('yion')
const app = createApp()
const server = createServer(app)
app.get('/', ({req, res}) => {
res.set('Content-Type', 'text/html; charset=utf-8').send('Hello world!');
});
server.listen(8080).on('listening', () => {
console.log('Server is running on port 8080')
})
Router
app.get(pattern, callback);
app.post(pattern, callback);
app.put(pattern, callback);
app.delete(pattern, callback);
app.patch(pattern, callback);
Parameters
app.get('/article/:id', ({res, res}) => {
let id = req.params.id
})
You can use regexp like this
app.get('/article/:id([0-9]{2})', ({res, res}) => {
let id = req.params.id
})
And of course both
app.get('/article/:id([0-9]{2})/:name', ({res, res}) => {
let id = req.params.id
let name = req.params.name
})
Body
const parseBody = require('yion/middlewares/parse-body')
app.post('/article', parseBody, ({res, res}) => {
let title = req.body.title || null
let content = req.body.content || null
})
Note : The body parser is very simple, it parse only x-www-form-urlencoded
and JSON data. Please see bodyParser
plugin for more features
Queries
// GET /articles?order=title&direction=asc
app.get('/article', ({res, res}) => {
let order = req.query.order || 'created_at'
let direction = req.query.direction || 'desc'
})
Middlewares
app.use(({req, res}, next) => {
// do stuff
next()
})
You can pass arguments to next middleware.
app.use((req, res, next) => {
// do stuff
next('foo', { foo: 'bar' })
})
app.get('/', ({req, res}, next, arg1, arg2) => {
console.log(arg1) // 'foo'
console.log(arg2) // { foo: 'bar' }
// do stuff
})
Every thing is a middleware
app.get('/', ({req, res}, next) => {
next({ foo: 'bar' })
})
app.get('/', ({req, res}, next, foo) => {
res.send(foo) // 'bar'
})
And you can compose middlewares into middleware
app.get(
'/admin',
({req, res}, next) => {
const session = getSession(req)
next(session)
},
({req, res}, next, session) => {
res.send(`Hello ${session.username}!`)
}
)
Assets
app.link('/css', __dirname + '/styles')
app.link('/js', __dirname + '/js')
app.link('/img', __dirname + '/images')
Now you can type into HTML file
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/main.js"></script>
</head>
</html>
You can add header at response
const cache = { 'Cache-Control': 'public, max-age=600' }
app.link('/css', __dirname + '/styles') // no cache
app.link('/js', __dirname + '/js', cache) // add cache control
app.link('/img', __dirname + '/images', cache) // add cache control
Plugins
const { createApp, createServer } = require('yion')
const { bodyParser } = require('yion/plugins')
const app = createApp()
const server = createServer(app, [bodyParser])
app.post('/file', (req, res) => {
if (!req.body.file) {
return res.status(500).send()
}
const file = req.body.file
res.sendFile(file.filepath, file.filename, file.mimetype)
})
server.listen(8080)
If you want to create a plugin, make a simple object with a handle
function.
There are 2 types of plugin :
Plugin handles POST/GET request, example (⚠️ type has to be post
):
const myPostPlugin = {
type: 'post',
handle: (context, app) => {
const { req, res } = context
const request = req.original // get Node Request
if (request.method === 'POST') {
// make stuff
}
app.dispatch(context) // dispatch request, plugins loop stop
}
}
And plugin add features into application, example :
const moment = require('moment')
const myMomentPlugin = {
type: 'whatever',
handle: (context, next) => {
context.moment = (date) => moment(date)
next(); // use next callback to call next plugin
// don't use app.dispatch(), because other plugins need to be launched
}
}
And into your Yion application
app.get('/what-time-is-it', ({moment}) => {
res.send(moment().format())
})
Plugins :
yion-body
: Body parseryion/plugins/body-parser
yion-logger
: Loggeryion/plugins/logger
Documentations and API Reference
You can see documentations here. Full API reference here. Also see yion websie
Factories
createApp()
: create a new applicationcreateServer(app, [])
: create a new server with an application and an array of plugins (optional)
Request
req.has(key)
: check if there are parameter or attribute withkey
req.get(key)
: get value of parameter or attribute withkey
Response
res.status(code, message = null)
: change HTTP statusres.set(key, value)
: set HTTP headerres.write(message, encoding = 'utf-8')
: add content into HTTP response bodyres.send(data = null, encoding = 'utf-8')
: send responseres.json(data, encoding = 'utf-8')
: send json responseres.redirect(location, code = 301)
: send redirect responseres.sendFile(filepath, filename, mimetype = "text/plain", attachment = true)
: Send file
2 months ago
2 months ago
2 months ago
2 months ago
7 months ago
8 months ago
9 months ago
1 year ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago