node-bigpipe
Introduction
- Bigpie module for nodejs, web frameworks like Express, Sails, ThinkJS
- Only 3 Simple API:
start
, pipe
, end
, very light and transparent - Based on
Promise
, easy to use and control
Install
- Backend side: [npm:
npm install node-bigpipe --save-dev
]. Or [Copy Files into your project directly] - Frontend side: Involve the
jQuery
and bro.bigpipe.js
scripts into html. Or require('node-bigpipe/static/bro.bigpipe')
by webpack
or browserify
. - You can write the Frontend js code by yourself, make sure to match the
Bigpipe
API of Backend.
Usage
In Backend, you should use node-bigpipe
to create a bigpipe for response.
In Frontend, you can use bro.bigpipe.js
, or use your own js code to call the bigpipe
from Backend.
Step:
- require the
node-bigpipe
module by var Bigpipe = require('node-bigpipe').Bigpipe
- Create an Bigpipe by
var bigpipe = new Bigpipe('pipeName', req, res)
- Use
start
api to ouput the pipe and render the unclosed base html frame - Use
pipe
api to transport to browser the array composed by pipe blocks you created - Use
end
api to finish this bigpipe - More detail in Example
Backend API
bigpipe.start(viewPath, [data])
: Start renderbigpipe.pipe(promiseList)
:transport the pipebigpipe.end([onEndFn])
: Stop and finish the pipebigpipe.render(selector, htmlData)
: Similar with $(selector).html(htmlData)
, set the dom html contentbigpipe.append(selector, htmlData)
: Similar with $(selector).append(htmlData)
, append the dom element contentbigpipe.fire(eventName, data)
: Trigger the event which subscribed in Front End. The event should is used to deal with the data transported by bigpipe. You can use on
api to subscribe the event.
Browser API
bigpipe.on(eventName).then(data=>{ // deal with data ... })
: Subscribe the eventName and you can use then
to add callback
fnbigpie.fire/render/append
Set the dom content, Same with mentioned above in Backend API.
Examples
- Create a pipe and return a Promise
The implementation will be put into the tagPipe, bp
function tagPipe(bp){
return new Promise((resolve, reject)=>{
let rdata = {
'tag': 'your data'
}
setTimeout(()=>{
let html = '<div><span>TagA</span><span>TagB</span><span>TagC</span><span>TagD</span></div>'
let pipeData = {
'html': html,
'message': 'for tag pipe html'
'css': ['a.css'],
'js': ['b.js'],
}
bp.fire('tag', pipeData)
resolve()
}, 3000)
})
}
- In Backend controller, start the pipe
index (req, res, next, page=1){
let bp = new Bigpipe('karatBP', req, res)
bp.start('view/home')
.pipe([
articlePipe,
tagPipe,
])
.end()
},
<script type="text/javascript">
new Bigpipe('karatBP')
</script>
Full Demo Code
- Full Backend demo code in
demo/DemoController.js
:
function tagPipe(bp){
return new Promise((resolve, reject)=>{
let rdata = {
'tag': 'your data'
}
setTimeout(()=>{
let html = '<div><span>TagA</span><span>TagB</span><span>TagC</span><span>TagD</span></div>'
let pipeData = {
'html': html,
'message': 'for tag pipe html'
'css': ['a.css'],
'js': ['b.js'],
}
bp.fire('tag', pipeData)
resolve()
}, 3000)
})
}
function articlePipe(bp){
return new Promise((resolve, reject)=>{
let rdata = {
'article': 'your data'
}
bp.res.render('view/article', rdata, (err, html)=>{
bp.render('.wrap > .content', html)
resolve()
})
})
}
export default {
index (req, res, next, page=1){
let bp = new Bigpipe('karatBP', req, res)
bp.start('view/home')
.pipe([
articlePipe,
tagPipe,
])
.end()
},
}
<script type="text/javascript" src="/static/jquery.min.js"></script>
<script type="text/javascript" src="/static/bro.bigpipe.js"></script>
<script type="text/javascript">
new Bigpipe('karatBP')
karatBP.on('tag')
.then(function (data) {
var pipeData = JSON.parse(data)
console.log(pipeData.message)
$('#tag').html(pipeData.html)
})
</script>
Support for ThinkJS
Difference of usage in Backend:
- Put an additional param
this
into new Bigpipe()
, like new Bigpipe('xxxBP', http.req, http.res, this)
- Other API is the SAME
- More About ThinkJS: https://thinkjs.org
export default class extends Base {
indexAction(){
let http = this.http;
let bp = new Bigpipe('thinkBP', http.req, http.res, this)
bp.start()
.pipe([
tagPipe,
testPipe
])
.end()
}
}
More
Please check the demo code in demo
folder in the repository. And the Frontend js code is in static
folder.