Giter Site home page Giter Site logo

moa2's Introduction

Moa 2 = Moajs with koa 2.x

Moa2 is a open-source web framework based koa 2.x、mongoose、bluebird、ava that’s optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration.

gitter NPM version

js-standard-style

"Lost, like the Moa is lost" - Maori proverb

技术栈

core

with database

Features

Core

  • 自动加载路由
  • 使用jsonwebtoken做用户鉴权
  • 支持migrate测试
  • 支持ava测试
  • 默认集成res.api,便于写接口
  • 集成supervisor,代码变动,自动重载
  • gulp自动监控文件变动,跑测试
  • gulp routes生成路由说明
  • 使用log4js记录日志

Env

  • production 产品模式,即部署到服务器上的
  • test 测试模式,辅助测试
  • development 开发模式,日志上会有差异

举例

$ export NODE_ENV=production && ./node_modules/.bin/supervisor ./bin/www

or

$ npm run production 

Getting Start

$ git clone --depth=1 https://github.com/moajs/moa2.git <your-project-name>
$ cd <your-project-name>
$ npm install
$ npm start

Koa 2支持3种中间件,其中async/await需要babel支持,根据koa、koa2、koa2-async和express的压测性能比较,性能不是特别好,所以默认Moa2不使用async/await

说明

  • user 是common function
  • student 是GeneratorFunction

浏览器里访问地址

使用脚手架

Install moag as Moajs scaffold

$ [sudo] npm install --global moag
$ cd <your-project-name>
$ moag <model-name> name:string password:string -k
  • for koa 2 common function
$ moag user name:string password:string -k
  • for koa2 generator
$ moag book name:string password:string -k -g
  • for koa2 async/await
$ moag cup name:string password:string -k -a

开启async/await

async/await需要babel,所以使用runkoa可以暂时作为开发用,不建议产品环境使用。

$ npm run async

Moajs微信用户组

如果加不上,请加微信shiren1118,说明加入原因

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

版本历史

  • v1.0.0 初始化版本,以Koa 2.x为基础

欢迎fork和反馈

如有建议或意见,请在issue提问或邮件

License

this repo is released under the MIT License.

moa2's People

Contributors

i5ting avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moa2's Issues

项目跑不起来

根据步骤
$ git clone --depth=1 https://github.com/moajs/moa2.git <your-project-name> $ cd <your-project-name> $ npm install $ npm start
发现没有bin/www文件,整个项目跑不起来。

npm test 报错

`require('../../config/log.js')

const jwt = require('jsonwebtoken')//用来创建和确认用户信息摘要

// 检查用户会话
module.exports = function (ctx, next) {
if (process.env.moas) {
ctx.api_user = {
_id : "55d8702d5472aa887b45f68c"
}
log('当前使用moas运行,不使用token即可访问!')
return next()
}

log('检查post的信息或者url查询参数或者头信息')

//检查post的信息或者url查询参数或者头信息
var token = req.body.token || req.query.token || req.headers['x-access-token']

// 解析 token
if (token) {
// 确认token
jwt.verify(token, 'app.get(superSecret)', function (err, decoded) {
if (err) {
return res.json({ success: false, message: 'token信息错误.' })
} else {
// 如果没问题就把解码后的信息保存到请求中,供后面的路由使用
ctx.api_user = decoded
dir(ctx.api_user)
return next()
}
})
} else {
// 如果没有token,则返回错误
ctx.status = 403
return ctx.body = {
success: false,
message: '没有提供token!'
}
}
}`

这里 req没有 声明,然后 我声明之后 就变成了
<-- POST /api/users xxx POST /api/users 500 9ms - TypeError: Cannot read property 'token' of undefined at module.exports (/Users/thomaslau/WebstormProjects/moa2/app/middlewares/check_api_token.js:25:23)

当路径中含有app时mount-middlewares报错。。

比如我git clone --depth=1 https://github.com/moajs/moa2.git moa2app ,
然后npm start就会报这种错:

Starting child process with 'node ./bin/www'
fs.js:856
  return binding.readdir(pathModule._makeLong(path));
                 ^

Error: ENOENT: no such file or directory, scandir '/Users/z/Projects/Node/moa2app/middlewares'
    at Error (native)
    at Object.fs.readdirSync (fs.js:856:18)
    at requireDirectory (/Users/z/Projects/Node/moa2app/node_modules/require-directory/index.js:59:6)
    at m (/Users/z/Projects/Node/moa2app/node_modules/mount-middlewares/index.js:20:10)
    at Object.<anonymous> (/Users/z/Projects/Node/moa2app/app.js:10:50)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (/Users/z/Projects/Node/moa2app/bin/www:7:11)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:140:18)
    at node.js:1001:3
Program node ./bin/www exited with code 1

然后debug下mound-middlewares源码

var fs = require('fs')
var requireDirectory = require('require-directory')
var debug = require('debug')('mount-middlewares')

function m (dir) {
  var a = dir.split('app') //竟然是用split的。。

  if (a.length > 1) {
    a.pop()
    a.join('app')
  }else {
    if(fs.existsSync(dir + '/app/middlewares')){
      return requireDirectory(module, dir + "/app/middlewares") 
    }

    throw "mount-middlewares ERROR: " + dir + "里没有app目录"
  }
  var _dir = a[0] + "app/middlewares"
  debug(_dir)
  return requireDirectory(module, _dir)
}

module.exports = m

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.