-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (60 loc) · 1.88 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//import express package
var express = require("express");
//import mongodb package
var mongodb = require("mongodb");
var path = require('path');
//Plotly
var charts = require('plotly')('vidhya0406', 'PJQCBSWERHz3XdRzMggK');
//MongoDB connection URL - mongodb://host:port/dbName
var dbHost = "mongodb://test:test@ds037195.mlab.com:37195/cryptocurrency";
var dbObject;
var MongoClient = mongodb.MongoClient;
MongoClient.connect(dbHost, function(err, db){
if ( err ) throw err;
dbObject = db;
});
// function to grab and format data from mongodb collection
function getData(responseObj){
dbObject.collection("cryptocurs").find({}).toArray(function(err, data){
if ( err ) throw err;
var dateArray = [];
var amountArray = [];
//Loop through the entires and grab the data
for (index in data){
var entry = data[index];
var date = entry['date'];
var amount = entry['amount'];
//push data to the arrays for plotting
dateArray.push(date);
amountArray.push(amount);
}
//Dataset for plotting
var dataset = [
{
x : dateArray,
y: amountArray,
fill: "tonexty",
type: "scatter"
}
];
var graphOptions = {filename: "bt-trend", fileopt: "overwrite"};
charts.plot(dataset, graphOptions, function (err, msg) {
console.log(msg);
});
});
}
//create express app
var app = express();
//NPM Module to integrate Handlerbars UI template engine with Express
var exphbs = require('express-handlebars');
//Declaring Express to use Handlerbars template engine with main.handlebars as
//the default layout
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
//view configs
app.use('/public', express.static('public'));
app.get("/", function(req, res){
getData();
res.sendFile(path.join(__dirname + '/index.html'));
});
app.listen(process.env.PORT || 3300);