Accepted answer

NOTE : You should be using some sort of template engine.

The below is just something to show that it's possible


<!DOCTYPE html>
    <meta charset="utf-8" />
        // ** entire Chart.js library **
    <canvas id="myChart"></canvas>
        var data = {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [
                    label: "My First dataset",
                    fillColor: "rgba(220,220,220,0.2)",
                    strokeColor: "rgba(220,220,220,1)",
                    pointColor: "rgba(220,220,220,1)",
                    pointStrokeColor: "#fff",
                    pointHighlightFill: "#fff",
                    pointHighlightStroke: "rgba(220,220,220,1)",
                    data: {{chartData}}

        var ctx = document.getElementById("myChart").getContext("2d");
        var myNewChart = new Chart(ctx).Line(data);

Notice the placeholder {{chartData}}. Also do note that you have to substitute in the actual script from the Chart.js file (you could link to the script file, but then you'll need a module that serves up static files)


var http = require('http');
var fs = require('fs');

http.createServer(function (req, response) {
    fs.readFile('index.html', 'utf-8', function (err, data) {
        response.writeHead(200, { 'Content-Type': 'text/html' });

        var chartData = [];
        for (var i = 0; i < 7; i++)
            chartData.push(Math.random() * 50);

        var result = data.replace('{{chartData}}', JSON.stringify(chartData));
}).listen(1337, '');

console.log('Server running at');

We simply substitute the placeholder with actual data.

Related Query

More Query from same tag