React.js hooks + koa.js 风格的轻量级 web 框架
React.js hooks + koa.js 风格的轻量级 web 框架。
相关文章
安装
koa-hooks requires node v10 or higher for ES2015 and async function support.
$ npm install @zhengxs/koa-hooks --save
示例代码
hello,world
import { use, listen } from '@zhengxs/koa-hooks'
use(async ctx => {
ctx.body = `hello,world`
})
listen(8080, () => {
console.log('Liston http://127.0.0.1:8080')
})
路由匹配 和 重定向
import { Server } from 'http'
import { use, route, redirect, urlFor, listen } from '@zhengxs/koa-hooks'
route('product.detail', '/api/product/:id', ctx => {
ctx.status = 200
ctx.type = 'application/json'
ctx.body = ctx.params
})
route('/api/user/info', () => {
console.log('用户未登陆,跳转到登陆页')
redirect(urlFor('login'))
})
route('login', '/login', ctx => {
ctx.body = 'This login page.'
})
use(ctx => {
ctx.body = `hello,world`
})
listen(8080, function onReady() {
console.log('Liston http://127.0.0.1:8080')
})
启动项目
你需要安装 node.js 的版本为 nodejs >= 8.0
。
克隆此仓库后运行:
# 安装依赖,推荐使用 yarn 安装
$ npm install
# 启动示例
$ npm run dev
在 package.json
文件的 scripts
部分还有一些其他脚本可用.
版本发布
# 更新版本号,内置代码检查
$ npm version <new_version|major|minor|patch>
# 发布代码包,自动代码
$ npm publish
可使用 npm version --help
查看帮助信息
感谢
感谢这些开源贡献给予的灵感,以下排名不分先后。