This example illustrates how to load the MRT dashboard template from the server side and send it to the client. First of all, you should add Stimulsoft modules and other required modules to the project. This can be done using the
require()
standard function:
var http = require("http");
var fs = require('fs');
// Stimulsoft Dashboards module
var Stimulsoft = require("stimulsoft-dashboards-js");
Next, you should define the
accept()
function that will process requests to the server and output the desired result at a specific URL address - an HTML page, script and style files, a dashboard template:
function accept(req, res) {
//Send index.html
if (req.url == "/") {
res.writeHeader(200, { "Content-Type": "text/html" });
res.end(fs.readFileSync("web/index.html"));
}
//Send style.css
else if (req.url == "/stimulsoft.viewer.office2013.whiteblue.css") {
res.writeHeader(200, { "Content-Type": "text/css" });
res.end(fs.readFileSync("web/stimulsoft.viewer.office2013.whiteblue.css"));
}
//Send reports.js
else if (req.url == "/stimulsoft.reports.js") {
res.writeHeader(200, { "Content-Type": "text/javascript" });
res.end(fs.readFileSync("web/stimulsoft.reports.js"));
}
//Send dashboards.js
else if (req.url == "/stimulsoft.dashboards.js") {
res.writeHeader(200, { "Content-Type": "text/javascript" });
res.end(fs.readFileSync("web/stimulsoft.dashboards.js"));
}
//Send viewer.js
else if (req.url == "/stimulsoft.viewer.js") {
res.writeHeader(200, { "Content-Type": "text/javascript" });
res.end(fs.readFileSync("web/stimulsoft.viewer.js"));
}
//Send dashboard
else if (req.url == "/getDashboard") {
// Creating new dashboard
var report = Stimulsoft.Report.StiReport.createNewDashboard();
// Loading dashboard template
report.loadFile("dashboard/Dashboard.mrt");
// Saving dashboard to JSON string
var dashboardJson = report.saveToJsonString();
//Send dashboard
res.end(dashboardJson);
}
}
All is ready, start the server and specify the required port, in this case, 8888:
console.log("Static file server running at http://localhost:" + 8888 + "/\nCTRL + C to shutdown");
//The HTTP server run on port 8888
http.createServer(accept).listen(8888);
In the screenshot below you can see the result of the sample code: