All Projects → Lxxyx → Koa2 Easy

Lxxyx / Koa2 Easy

Easy way to use koa2

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Koa2 Easy

Koa Sslify
Enforce HTTPS in node.js koa apps
Stars: ✭ 100 (-15.25%)
Mutual labels:  koa
Artalk
🌌 一款简洁有趣的可拓展评论系统 | A Fast, Slight & Funny Comment System.
Stars: ✭ 110 (-6.78%)
Mutual labels:  easy-to-use
Deskreen
Deskreen turns any device with a web browser into a secondary screen for your computer
Stars: ✭ 12,014 (+10081.36%)
Mutual labels:  easy-to-use
Chatroom
vue2聊天室,图灵机器人,node爬虫
Stars: ✭ 103 (-12.71%)
Mutual labels:  koa
Strapi
🚀 Open source Node.js Headless CMS to easily build customisable APIs
Stars: ✭ 41,786 (+35311.86%)
Mutual labels:  koa
Koa React Universal
lightweight React-Koa2 universal boilerplate, only what is essential
Stars: ✭ 112 (-5.08%)
Mutual labels:  koa
Art Template
High performance JavaScript templating engine
Stars: ✭ 9,554 (+7996.61%)
Mutual labels:  koa
Swiftui Sample App
This swiftUI Demo is very simple & easy to understand. This swiftUI demo includes On-boarding screens, login screen, forgot password screen, sign up screen, home & logout.
Stars: ✭ 117 (-0.85%)
Mutual labels:  easy-to-use
Cli
Get a programmable email address. Automate what happens when you receive emails. It's like Zapier for devs who hate emails.
Stars: ✭ 105 (-11.02%)
Mutual labels:  easy-to-use
Herostory
王者荣耀故事站--小程序
Stars: ✭ 115 (-2.54%)
Mutual labels:  koa
Cottage
Simple, fast HTTP router on koa.js.
Stars: ✭ 103 (-12.71%)
Mutual labels:  koa
Koa Pug
A Pug middleware for Koa
Stars: ✭ 105 (-11.02%)
Mutual labels:  koa
Portfolio
My self coded personal website build with React.js
Stars: ✭ 112 (-5.08%)
Mutual labels:  easy-to-use
Koa Mobx React Starter
A straightforward starter for Node javascript web projects. Using Koa, MobX and ReactJS (with universal / isomorphic server rendering)
Stars: ✭ 102 (-13.56%)
Mutual labels:  koa
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+9194.07%)
Mutual labels:  koa
Vue Family Bucket Ssr Koa2 Full Stack Development From Meituan
🚀🚀2020最新Vue全家桶+SSR+Koa2全栈开发☁
Stars: ✭ 100 (-15.25%)
Mutual labels:  koa
Wow64pp
A modern c++ implementation of windows heavens gate
Stars: ✭ 109 (-7.63%)
Mutual labels:  easy-to-use
Koalerplate
Simple Koa Boilerplate for APIs
Stars: ✭ 118 (+0%)
Mutual labels:  koa
Node Frameworks Benchmark
Simple HTTP benchmark for different nodejs frameworks using wrk
Stars: ✭ 117 (-0.85%)
Mutual labels:  koa
Hongbao Bot2
饿了么红包微信机器人(不再维护)
Stars: ✭ 114 (-3.39%)
Mutual labels:  koa

注意:

请使用Node6以上版本,获得更好的性能与ES6支持。 启用Babel-es2017,不再对一些Node6已支持的特性转码。

起因

因为Koa2已经在蓬勃发展中,Async/Await能切实的避免回调地狱。所以基于Koa2订制了一套模板。

全局安装koa2-easy,之后使用koa2命令即可在当前目录一键生成服务端程序。地址为:当前目录/koa2-easy

npm i koa2-easy -g

koa2

cd koa2-easy && npm i (推荐使用cnpm)

功能

  1. 渲染模板(artTemplate
  2. 发送静态文件,如HTML文件。
  3. 编写自定义路由与Restful Api,默认支持CORS跨域
  4. 读取post数据
  5. 纯ES6/7编写,使用koa2的Async/Await,避免回调地狱
  6. 默认支持gzip,减少传输体积,加快传输速度
  7. 支持文件上传
  8. 开箱即用,无需折腾
  9. 支持TypeScript模板

启动

直接运行run.js即可
因为Koa2使用babel和ES7的Async/Await,所以需要使用babel转译启动,所以必须运行run.js,而非app.js。

node run.js

开发模式

检测到文件变动,会自动重启服务器

npm i
npm run dev

服务器部署

npm i pm2 -g
npm run pm2

指定端口启动

PORT=8000 npm run dev
PORT=8000 npm run pm2

把8000替换成任何你想使用的端口即可

配合Nginx实现反向代理(可选)

推荐使用VeryNginx实现。

具体配置(需要先安装VeryNginx): 先配置一个 Matcher : host=app.xxxx.com ,用来提取出来所有访问 host 为 app.xxxx.com 的请求 然后配置 Proxy Pass 把这个请求转发到 127.0.0.1:3000 就可以了

使用

需要具有ES6基础。

模板渲染

// router/index.js
router
  .get('/', async ctx => {
    // 模板渲染,第一个参数为模板名称
    // 模板放置于views文件夹中
    ctx.body = ctx.render('index', { title: 'Koa2-Easy' })
  })

发送静态HTML文件

// router/index.js
router
  .get('/index', async (ctx, next) => {
    // 发送静态文件
    await ctx.send(ctx, 'index.html', { root: 'static/index' })
  })

HTML文件中,静态文件的路径处理

把HTML文件放入static文件夹中。单独开辟文件夹。
如首页index.html,则放入/static/index文件夹。引用CSS等文件时,需加入文件夹名称。

<!-- /static/index/index.html -->
<link rel="stylesheet" type="text/css" href="/index/css/index.css">

发送JSON数据

直接将ctx.body设置为json格式即可

// router/test.js
router
  .get('/json', (ctx, next) => {
    ctx.body = { test: 'json' }
  })

读取post数据

ctx.request.body就是post发送的数据

// router/test.js
router
  .post('/post', (ctx, next) => {
    ctx.body = ctx.request.body
  })

添加新路由

按照router文件夹中范例编写,并在app.js中添加即可。

// 添加/api路由

// 将api前缀设置为/api
const router = new Router({
  prefix: '/api'
})

// 对Api进行操作
router
  .get('/', (ctx, next) => {
    ctx.body = "This is Api page"
  })
// 在app.js中引用
import api from './router/api'
app.use(api.routes())
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].