to bind dynamic data to web content page
npm install data-binder
The Objective is to enable dynamic data binding to web content to send as response .
----------
Overview
-------------
All dynamic data can be added to file/response by mapping the required data to variable.
All variables should be declared inside curly braces preceeded by $ in source file.
>Input data(index.html) :
<html>
<head>
<head>
<body>
<span>Hi, ${name} here.<span>
<body>
</html>
>Expected output(indexTemp.html) :
<html>
<head>
<head>
<body>
<span>Hi, Naveen here.<span>
<body>
</html>
Available methods are as follows :
----
Method 1:
To achieve data binding and to get result back in given destination file.
>'use strict'
> dataBinder = require('data-binder');
>var dataArray = [] //Array should be created to add dynamic data and respective keys
>dataArray['name'] = "Naveen" //The dynamic data "Naveen" is attached to variable "name"
>/ method signature : bindToDestinationFile(sourceFilePath, dynamicDataArray, destinationFilePath, callback(optional)) this method will binds data and then reflects the required data in destination file. /
>dataBinder.bindToDestinationFile("./index.html",dataArray, "./indexTemp.html")
Method 2:
To achieve data binding and to pipe data into given WriteStream.
> 'use strict'
> var fs = require('fs'),
> path = require('path'),
> dataBinder = require('data-binder');
> var dataArray = []
> dataArray['name'] = "Naveen"
> / method signature : bindToWriteStream (sourceFilePath, dynamicDataArray, destinationWriteStream, callback(optional)) this method will binds data and pipe the required data into destination WriteStream. /
> var destinationWriteStream = fs.createWriteStream(path.normalize("./indexTemp.html"))
> dataBinder.bindToWriteStream ("./index.html", dataArray, destinationWriteStream)
Method 3:
To achieve data binding and to pipe data into given Response object.
> 'use strict'
> var dataBinder = require('data-binder');
> var dataArray = []
> dataArray['name'] = "Naveen"
> / method signature : bindToResponse (sourceFilePath, dynamicDataArray, response, callback(can be used to handle errors)) this method will binds data and pipe the required data into response /
> dataBinder.bindToResponse ("./index.html", dataArray, response)
Method 4:
To achieve data binding and to pipe data into given Response object with custom header object.
> 'use strict'
> var dataBinder = require('data-binder');
> var dataArray = [], responseHeader = {}
> dataArray['name'] = "Naveen"
>responseHeader = {
'Content-Type' : 'text/html; charset=utf-8',
'X-Content-Type-Options': 'nosniff',
'Cache-Control' : 'public, max-age=3600, no-transform, immutable',
'Content-Encoding': 'gzip',
'Vary' : 'Accept-Encoding',
'Strict-Transport-Security' : 'max-age=31536000; includeSubDomains',
'X-Frame-Options' : 'SAMEORIGIN',
'X-XSS-Protection' : '1; mode=block; report=localhost:1337/reports/security/log',
'ETag' : 'abcdef'
}
> / method signature : bindToCustomOkResponse (sourceFilePath, dynamicDataArray, response, responseHeader, callback(can be used to handle errors)) this method will binds data and pipe the required data into response /
> dataBinder.bindToCustomOkResponse ("./index.html", dataArray, response, responseHeader, function(errMsg){
if(errMsg){
res.writeHead(401, "{'Content-Type' : 'text/html'}")
res.end()
}
});