express-middleware-bigpipe
express' bigpipe Middleware.
Start
Install
npm i express-middleware-bigpipe --save
Use in express
// express codevar bigpipe = app
Usage:
本中间件的 namespace 在 res.bigpipe 下, 所有方法都支持链式调用, 输出完成后自动触发res.end()
结束请求.
The namespace of this middleware is at res.bigpipe
, all methods allow chained call, it will automatically invoke res.end()
to end a request when finished pipe.
write(str);
直接输出, 与res.end
类似, 但是不会终止请求
directly pipe a string
pagelet(file, errHandler);
读取basedir下的file文件, 第二个参数为错误处理回调, 传入参数err
pipe the static file in opts.basedir
, errHandler is a callback function to handle errors.
render(view, renderObj, callback/* optional */);
使用模板引擎读取模板目录下模板, renderObj 为渲染的数据, callback回调采用尾触发, 传入一个参数assign, 在callback中异步获取到数据之后使用assign(obj) 将数据合并入renderObj并渲染输出.
This method use template engine to bigpipe a rendered string.
The callback function have an assign
argument which is a function to assign an obj to render asynclly.
该方法可用的重载:
You can also use this method in the following way:
render(view, renderObj);
render(view, callback);
done(fn);
, done(str);
提前结束, 相当于 res.end , 可以传入 String 直接输出, 或者回调函数在 res.end 之前执行.
End the request immediately.
You can also pass a function or string. If it is a func, it will be called before res.end, or if it is a string, it will be passed to res.end(str)
.
Quick Demo:
var app = bigpipe = ; // import //... // view engine setupapp;app; app; app;
Test
npm test
Use mocha and supertest
By Ling created @ 2015-04-29 21:10:36 www.zeroling.com
Follow me at https://github.com/wssgcg1213
Report issue at https://github.com/wssgcg1213/express-middleware-bigpipe/issues