Coder Social home page Coder Social logo

xiongwilee / gracejs Goto Github PK

View Code? Open in Web Editor NEW
1.4K 66.0 238.0 9.9 MB

A Nodejs BFF framework, build with koa2(基于koa2的标准前后端分离框架)

Home Page: https://grace.wilee.me

License: MIT License

JavaScript 63.52% HTML 6.70% CSS 26.29% Dockerfile 0.03% SCSS 3.46%
koa nodejs mvc proxy sfb framework grace gracejs koa-grace

gracejs's People

Contributors

dependabot[bot] avatar etheranl avatar ewxm avatar thunf avatar xiongwilee 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  avatar  avatar  avatar  avatar

Watchers

 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

gracejs's Issues

koa 新手求教

if (ctx.proxy) return await next();

中间件里像 把proxy这种功能绑在 ctx上 这种方式真的好吗? 这种工具往往不变,每次请求过来这个操作都要执行一遍,而且代码里每次绑定时都是重新声明。 另外中间件 开头判断ctx.proxy 是否存在,这个操作本身就没用吧。。每次请求过来,ctx都是重新实例化的,不改原型的话,肯定不存在啊。

Mock配置

demo里能不能加个mock的配置,文档里的看得不是很清楚~

配置mongoDB配置路径

你好,如文档中所说:

修改配置项:config.mongo.blog 为您的本地mongoDB的路径:
// mongo configuration mongo: { 'blog': 'mongodb://localhost:27017/blog' }


我的mongoDB安装路径如下:
C:\Program Files\MongoDB
C:\Program Files\MongoDB\Server\3.2\bin

{ 'blog': 'mongodb:_' },_这里的路径需要怎么填?

Nunjucks自定义选项不起作用

Nunjucks自定义选项不起作用, 是以下这个文件缺少了配置项引起的

node_modules\grace-consolidate\lib\consolidate.js(1119行)

要将

if (options.settings && options.settings.views) engine.configure(options.settings.views);

修改为:

if (options.settings && options.settings.views) engine.configure(options.settings.views, options.settings.options);

this.proxy请求

我要post一串xml给后台服务器,但我发现你的配置项里只支持form啊,header也不能配置

webstrom run || debug 模式跑不起来

/Users/liu/Desktop/CssLearn/nodejs/koa-grace/src/app.js:54
config.csrf.module.indexOf(appName) > -1 && vapp.use(csrf(vapp, {
^

TypeError: Cannot read property 'module' of undefined
at app.use.vhosts.map (/Users/liu/Desktop/CssLearn/nodejs/koa-grace/src/app.js:54:14)
at Array.map (native)
at Object. (/Users/liu/Desktop/CssLearn/nodejs/koa-grace/src/app.js:47:22)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at require (internal/module.js:20:19)
at Object. (/Users/liu/Desktop/CssLearn/nodejs/koa-grace/bin/server.js:13:13)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

proxy代理数据问题

大神,你好!我想代理第三方接口,使用this.proxy('college_api:admin/class/class-list?userAccount=admin&token=xxx');为什么参数无法传到api里去呢?可以大概的说明一下,可能有哪些原因导致呢?感激!!!

模板引擎改为 nunjucks 出现找不到模板错误

结合了 grace-vue-webpack-boilerplate
views 目录结构为初始目录结构
views
-- _common
----hook
----layout.html
----...
--home
----index.html

views/home/index.html 内容

{% extends "../_common/layout.html" %}
...

报错信息:

[nodemon] changes after filters (before/after): 1/0
  koa-router GET /home +7m
  koa-router dispatch /home /^\/home(?:\/(?=$))?$/i +0ms
Unhandled rejection Template render error: (unknown path)
  Error: template not found: ../_common/layout.html
    at Object.exports.prettifyError (/Users/jeff/Code/new-wap/server/node_modules/nunjucks/src/lib.js:34:15)
    at /Users/jeff/Code/new-wap/server/node_modules/nunjucks/src/environment.js:489:31

居然找不到 layout.html,看路径是没错啊 😱
另模板引擎改回 swiger 便不会报错 😳

`defaultCtrl.js` 里做鉴权时抛出`401`错误,无法进入default err handing!

router.js 里捕获了错误,抛出的错误无法进入koadefault error handing 中。

middleware/router/lib/router.js

......
       while (ii--) {
          const strckFun = layer.stack[ii];
           try {
            if (strckFun.constructor.name === 'GeneratorFunction') {
              await co(strckFun.bind(ctx))
            } else {
              await strckFun.call(ctx);
            }
           } catch (err) {
             error(err);
             ctx.status = 500;
             ctx.body = 'Controller Execute Error!'
           }
        }
......

app/shop/controller/defaultCtrl.js

const { sid } = require('../../rds');
const level = require('../../../config/level.json');

module.exports = async function () {
	console.log('this is a base controller!');
......
		let xxxxx = await sid.hexists(`xxx:${token}`,'xxxxx');
		if(xxxxx){
			console.log('授权通过!')
		}else{
			const err = new Error(JSON.stringify({ code:Constant.statusCode.FAIL.code, msg: Constant.httpCode.Unauthorized.msg }));
			err.status = 401;
			err.expose = false;
			throw err;
		}
......
}

// 设置为非路由
module.exports.__controller__ = false;

koa/lib/context.js

  /**
   * Default error handling.
   *
   * @param {Error} err
   * @api private
   */

  onerror(err) {
    // don't do anything if there is no error.
  ...
    // force text/plain
    this.type = 'text';

    // ENOENT support
    if ('ENOENT' == err.code) err.status = 404;

    // default to 500
    if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;

    // respond
    const code = statuses[err.status];
    const msg = err.expose ? err.message : code;
    this.status = err.status;
    this.length = Buffer.byteLength(msg);
    this.res.end(msg);
  }
};

正则路由

比如说我要生成这样一个路由: http://127.0.0.1/company/development
company是controller目录下的一个路由文件,而后面的development是不确定的分类,我本想在company.js下设置__regular__为正则匹配, 结果正则并不起作用

github oauth 配置问题

源码里的 回调URL填的是网站的,不能用,自己申请的,授权都不成功,回调URL是有什么格式吗
message: "Not Found"

defaultCtrl 的作用

1.是用來當作filter使用嗎?
需要在每個controller的方法中 調用 yield bindDefault() , 不太方便。

2.以下這種寫法 調用不到 bindDefault() 方法。
exports.index_1 = async function () {
await this.bindDefault();
}

proxy的500友好页怎么跳转

当我请求后端 返回错误500的时候 我需要跳转到500的错误提示页面 这个要在哪里做 需要修改中间件的哪部分 。

nice。。。

// mongo configuration
mongo: {
'blog': 'mongodb://localhost:27017/blog'
}

但是这个 现在应该是
mongo: {
'api': 'mongodb://localhost:27017/blog'
}
吧?别忘了更新文档 `(∩_∩)′

还有 访问127.0.0.1:3000 直接404了

话说啥时候升级到koa2呢

Proxy-Post传XML参数时会被放进Form中。

请求参数

{
    "form": "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:bsas="http://siebel.com/loyalty/BSAssociateMemberWS">
   <soapenv:Header/>
   <soapenv:Body>
      <bsas:AssociateMemberCreated>
         <AssistantId>xxx</AssistantId>
         <SSOID>xxx</SSOID>
         <Channel>xxxx</Channel>
         <Phone>xxx</Phone>
         <Brand>xxx</Brand>
         <StoreNum>xxx</StoreNum>
      </bsas:AssociateMemberCreated>
   </soapenv:Body>
</soapenv:Envelope>",
    "gzip": true,
    "headers": {
        "Content-Type": "text/xml",
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "cache-control": "no-cache",
        "connection": "keep-alive",
        "content-type": "application/x-www-form-urlencoded",
        "cookie": "22222=20111196",
        "host": "xxxxxxxxxxxx",
        "postman-token": "15c7d414-a53d-4eba-bf18-31af59f1c17f",
        "user-agent": "PostmanRuntime/3.0.11-hotfix.2",
        "user-host": "test:3000"
    },
    "json": true,
    "method": "POST",
    "timeout": 15000,
    "uri": "http://xxxxxx/xx/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=xxx&UserName=xxxxxx&Password=xxx"
}

版本号

"version": "2.0.5"

注释掉json后发送就成功了

 // 获取request参数
  let opt = Object.assign({
    uri: undefined, // 请求路径
    method: undefined, // method
    headers: undefined, // 头信息
    gzip: true, //是否gzip
    timeout: 15000, // 超时时间
    // json: json, // json数据
    form: form // post的form参数,默认为undefined
  }, options);
    console.log("opt: "+JSON.stringify(opt))

mongodb

@xiongwilee :你好!
// mongo配置
mongo: {
options: {
// mongoose 配置
},
api: {
'blog': 'mongodb://localhost:27017/blog'
}
}
这个配置的意思,可以理解为blog是数据库的库名,对吧?

ctx对象上没有mongo方法?

  return async function mongo(ctx, next) {
        if (ctx.mongo) return await next();
        Object.assign(ctx, {
            mongo: function(mod, data) {
                if (!Model || !Model[mod]) {
                    debug("can't find model : " + mod);
                    return;
                }
                return (new Model[mod](data));
            },
            mongoMap: function(list) {
                return Promise.all(list.map(mongoExecMap));

                function mongoExecMap(opt) {
                    let arg = opt.arg || [];
                    let model = opt.model;
                    let fun = opt.fun;
                    let execfun = (fun.constructor.name === 'GeneratorFunction') ? co.wrap(fun) : fun;
                    let execRes = execfun.apply(model, arg);
                    return execRes.exec ? execRes.exec() : execRes;
                }
            }
        })
        await next();

我在中间添加了wechat的中间件作用自动拉取access_token,以及自动拉取用户列表数据

  1. 文档中是在controller中使用this调用,我希望在wechat中间件中链接mongo(在每天定点同步关注用户列表)上面这个代码我console.log(ctx.mongo)结果是undefined,如何在中间件中调用mongo
  2. 这个router这个中间件资质愚钝,实在是不太懂,, ctx和this实在是搞不明白.官方文档不是都可以用么但是这里的ctx和this是有区别的,就上面这个mongo的中间件.我看到object.assign已经将mongo合并到ctx上了..但是没什么console ctx还是没有

nunjucks 要添加个过滤器怎么加呢?

nunjucks 要添加个过滤器怎么加呢?

项目中需要用到不少过滤器,比如时间,货币等。。。看到nunjucks本身是支持的,要怎么结合进来呢?

var nunjucks = require('nunjucks');
var env = new nunjucks.Environment();

env.addFilter('shorten', function(str, count) {
    return str.slice(0, count || 5);
});

关于vhost: '127.0.0.1/test':'demo_test'

首先,非常感谢你提供这么棒的项目!
对于vhost有点疑问,如果这么配置'127.0.0.1':'blog',不会出现问题,现在我配置'127.0.0.1/other':'other',但是 127.0.0.1:3000/other 无法访问。
再解释下就是,希望127.0.0.1:3000访问blog项目,通过127.0.0.1:3000/other访问other项目。
还有就是能不能贴一下你的blog的配置呢?

数据库支持扩展

Gracejs目前支持的数据库好像是mongoDB,能不能写一个扩展接口,实现多数据库选择。比如mysql,谢谢!

路由的问题

我在用你这个框架的时候遇到个路由的问题,现在设计两个api,

  1. /api/topup 方法是get
  2. /api/topup/:id, 方法是post
    请问我该怎么在Controller里写这个路由呢?

路由可以使用别的模式

路由可以使用别的模式

要针对SEO做优化,路由要添加.html后缀,不知道要怎么弄,如:www.xxxx.com/user/index.html 这样子的

this.proxy 丢失headers内容

在使用 this.proxy('http://jsonplaceholder.typicode.com/users?_page=1') 时,发现直接调用中返回的

headers: {
Access-Control-Allow-Credentials:true
Access-Control-Expose-Headers:X-Total-Count, Link
Cache-Control:public, max-age=14400
CF-Cache-Status:REVALIDATED
CF-RAY:33305cd0d6872246-LAX
Connection:keep-alive
Date:Sat, 18 Feb 2017 09:13:21 GMT
Etag:W/"160d-MxiAGkI3ZBrjm0xiEDfwqw"
Expires:Sat, 18 Feb 2017 13:13:21 GMT
Link:
Pragma:no-cache
Server:cloudflare-nginx
Vary:Accept-Encoding
Via:1.1 vegur
X-Content-Type-Options:nosniff
X-Powered-By:Express
X-Total-Count:10
}

而 this.proxy 的返回只有这些

headers: {
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Sat, 18 Feb 2017 09:05:13 GMT
set-cookie:__cfduid=d8215742543560aabf718ebc4a66c415f1487408713; expires=Sun, 18-Feb-18 09:05:13 GMT; path=/; domain=.typicode.com; HttpOnly
Transfer-Encoding:chunked
Vary:Accept-Encoding
}

请问该怎么解决这个问题

运行报错了

const { res } = this;
^

SyntaxError: Unexpected token {
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object. (G:\html5\Koa\koa-grace\node_modules\koa\lib\application.js:11:18)
at Module._compile (module.js:398:26)
at Object.Module._extensions..js (module.js:405:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Module.require (module.js:354:17)
at require (internal/module.js:12:17)
at Object. (G:\html5\Koa\koa-grace\src\app.js:4:13)
at Module._compile (module.js:398:26)

如何提高单点TPS, QPS

试着用jmeter 30个并发,将GraceJs proxy代理到www.baidu.com, 发现throughput只有140左右,请问怎么提升单节点的吞吐量呢?

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.