I'm working with Node.js and trying to display a chart, generated out of coordinates from a txt file, which is uploaded to the server. My problem: When I open the web page and upload the file, everything works perfectly, but the chart is not working: I get the following error: UncaughtSyntaxError: Unexpected token var.
If I render the webpage without node.js everything works fine.
The code:
var express = require("express");
var app = express();
var fs = require('fs'); // we will need it for file uploads
var port = process.env.PORT || 3000;
app.configure(function(){
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.get('/', function(req, res) {
res.send('<form method="post" enctype="multipart/form-data">'
+ '<p>Image: <input type="file" name="uploadfile" /></p>'
+ '<p><input type="submit" value="Upload" /></p>'
+ '</form>');
});
app.post('/', function(req, res){
// .. Upload and file reading
// .. var obj[key1] (X-Values) and var obj[key2] (Y-Values) are generated
// ..
res.send('<head><title>Line Chart</title><script src="../Chart.js"></script><meta name = "viewport" content = "initial-scale = 1, user-scalable = no"><style>canvas{ }</style></head><body><canvas id="canvas" height="450" width="600"></canvas>'
+'<script type="text/javascript">'
+' '
+'var lineChartData = { '
+'labels : [ ' + obj[key1] + ' ],'
+'datasets : [{'
+'fillColor : "rgba(151,187,205,0.5)",'
+'strokeColor : "rgba(0, 0, 0, 0.831373)",'
+'pointColor : "rgba(151,187,205,1)",'
+'pointStrokeColor : "gba(76, 255, 178, 0.831373)",'
+'data : ['+ obj[key2] + ' ]'
+'} ] } '
+'var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Line(lineChartData);'
+'</script>'
+'</body></html>'
);
};
});
});
});
app.listen(port, function() {
console.log("Listening on " + port);
});
I hope you can help me..
Greetings,
JS