Express middleware that allows streaming of template literals w/ interpolation of Node.JS streams, Arrays and Promises.
$ npm install express-template-stream --save
Creates a new Express Template Stream middleware instance.
- [
options
] (Object) :- [
debug
] (Boolean): Defaults tofalse
. Iftrue
all requests will have anx-ets-streamed
header added. Its value being a binary 1 or 0 depending on if the request was streamed or not. Also setsthis.debug
within StreamTemplate Instances. - [
minify
] (Boolean): Defaults totrue
. Minify HTML - [
templates
] (Object): Consisting of all the possible StreamTemplates that can be used. - [
headers
] (Function|Object): Headers to be added to every request regardless of the StreamTemplate used. (Function must return an object) - [
match
] (Function): Expects an object returned with the shape of{ templateKey: string, templateData: any }
templateData can be accessed viathis.data
within StreamTemplates Instances.
- [
export default ETS.middleware({
debug: process.env.NODE_ENV === 'development',
minify: process.env.NODE_ENV === 'production',
headers: (req, res) => {
return {
...ETS.defaultHeaders,
'x-server-hash': ...
}
},
templates: {
amp: // AMP specific render template
normal: // render template
bot: // bot specific render template
},
match: (req, res) => {
return {
templateKey: req.useragent.includes('bot') ? 'bot' : 'normal',
templateData: {
...
}
}
}
})