xiongwilee / gracejs Goto Github PK
View Code? Open in Web Editor NEWA Nodejs BFF framework, build with koa2(基于koa2的标准前后端分离框架)
Home Page: https://grace.wilee.me
License: MIT License
A Nodejs BFF framework, build with koa2(基于koa2的标准前后端分离框架)
Home Page: https://grace.wilee.me
License: MIT License
Gracejs/middleware/proxy/index.js
Line 52 in 8ad4d95
demo里能不能加个mock的配置,文档里的看得不是很清楚~
我客户端的 例如 $,get("/text/a/b") 服务器我想转发到 baidu.com/a/b 要怎么配置啊!求教
你好,如文档中所说:
修改配置项: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:_' },_这里的路径需要怎么填?
如题
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);
如题:我启动了服务后,没想到我一修改服务器文件,在浏览器尽然可以立即看到效果了,是怎么做到的?
O(∩_∩)O谢谢
我要post一串xml给后台服务器,但我发现你的配置项里只支持form啊,header也不能配置
/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
每次有人请求一次,就打印一下他请求的地址.
大神,你好!我想代理第三方接口,使用this.proxy('college_api:admin/class/class-list?userAccount=admin&token=xxx');为什么参数无法传到api里去呢?可以大概的说明一下,可能有哪些原因导致呢?感激!!!
通过使用 this.proxy 访问java后端api接口时,取不到返回的statusCode值呢?
npm run start之后访问http://127.0.0.1:3000/ 页面返回Invalid hostname!
未对代码做修改
结合了 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 便不会报错 😳
router.js
里捕获了错误,抛出的错误无法进入koa
的 default error handing
中。
......
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!'
}
}
......
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;
/**
* 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);
}
};
3ks
比如说我要生成这样一个路由: http://127.0.0.1/company/development
company是controller目录下的一个路由文件,而后面的development是不确定的分类,我本想在company.js下设置__regular__为正则匹配, 结果正则并不起作用
源码里的 回调URL填的是网站的,不能用,自己申请的,授权都不成功,回调URL是有什么格式吗
message: "Not Found"
一个页面有好多post请求,一个个写感觉不美观,能整合到proxy里吗?是要修改proxy中间件了吧
1.是用來當作filter使用嗎?
需要在每個controller的方法中 調用 yield bindDefault() , 不太方便。
2.以下這種寫法 調用不到 bindDefault() 方法。
exports.index_1 = async function () {
await this.bindDefault();
}
还能更新吗,session中间件报错,this.session是undefined
当我请求后端 返回错误500的时候 我需要跳转到500的错误提示页面 这个要在哪里做 需要修改中间件的哪部分 。
在执行this.proxy 前,修改 content-type 为 application/json 呢
如果可以增加 eslint 等工具就更完善了.
如题目描述,我觉得该行配置应该是 ./app
代码里路径为 ../app
不知是否为我理解错误
// mongo configuration
mongo: {
'blog': 'mongodb://localhost:27017/blog'
}
但是这个 现在应该是
mongo: {
'api': 'mongodb://localhost:27017/blog'
}
吧?别忘了更新文档 `(∩_∩)′
还有 访问127.0.0.1:3000 直接404了
话说啥时候升级到koa2呢
nunjucks 自定义过滤器 应该写在什么文件里面,如何获取nunjucks对象,请指教
{
"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))
@xiongwilee :你好!
// mongo配置
mongo: {
options: {
// mongoose 配置
},
api: {
'blog': 'mongodb://localhost:27017/blog'
}
}
这个配置的意思,可以理解为blog是数据库的库名,对吧?
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();
项目中需要用到不少过滤器,比如时间,货币等。。。看到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':'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的配置呢?
this.bindDefault方法 在哪里定义的?我没有找到这个方法~/(ㄒoㄒ)/~~
RT.
nodemon每次都是重启node http server,很不方便.
能否有其他方案实现真正的代码热更新?
node版本: v8.1.4
模板引擎: Nunjucks
错误信息:
/middleware/proxy/index.js:390
ctx.res._headers['set-cookie'] = ctx.res._headers['set-cookie'].concat(cookies);
^
TypeError: Cannot read property 'concat' of undefined
执行 npm run dev
命令 controller 中有错误但控制台不报错
Gracejs目前支持的数据库好像是mongoDB,能不能写一个扩展接口,实现多数据库选择。比如mysql,谢谢!
我在用你这个框架的时候遇到个路由的问题,现在设计两个api,
想集成mysql,能推荐下吗
exports.koagrace = funtion* (){
this.body = 'hello koa-grace!';
}
function单词拼错了
要针对SEO做优化,路由要添加.html后缀,不知道要怎么弄,如:www.xxxx.com/user/index.html 这样子的
this.request.body不能接收xml格式的参数吗?
在使用 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
}
请问该怎么解决这个问题
如何配置能省略掉路由的文件夹名字呢?比如说在controller中有一个文件夹routers里面都是项目路由,在页面输入URL时候就是localhost:3000/routers/eg... 请问怎么配置这个routers可以省略掉
controller下面必须得有defaultCtrl.js这点没有说明!
你的微信或者qq是多少啊 或者整个论坛 qq群什么的 可以交流讨论
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)
试着用jmeter 30个并发,将GraceJs proxy代理到www.baidu.com, 发现throughput只有140左右,请问怎么提升单节点的吞吐量呢?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.