From 73e7e23dde2a45819d791d4fa54c4c38ad5b534f Mon Sep 17 00:00:00 2001 From: jrtechs Date: Tue, 9 Apr 2019 20:26:52 -0400 Subject: [PATCH] Made static file server in node serve the home page. --- .idea/vcs.xml | 6 +++++ index.html | 45 --------------------------------- server.js | 70 +++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 56 deletions(-) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/index.html b/index.html index 5116271..2b7363a 100644 --- a/index.html +++ b/index.html @@ -17,54 +17,9 @@
- - - - -
powered by Surfing Waves
-
- - \ No newline at end of file diff --git a/server.js b/server.js index e16e09a..aaac784 100644 --- a/server.js +++ b/server.js @@ -73,23 +73,71 @@ app.get('/error', (request, result) => }); -app.get('/', (request, result) => +const queryAPI = function(result, path) { - if(accessTokenTemp == null) + return new Promise((resolve, reject)=> { - result.redirect('/auth/fitbit'); - } + if(accessTokenTemp == null) + { + result.redirect('/auth/fitbit'); + resolve(false); + } + + unirest.get(path) + .headers({'Accept': 'application/json', 'Content-Type': 'application/json', Authorization: "Bearer " + accessTokenTemp}) + .end(function (response) + { + if(response.hasOwnProperty("success") && response.success == false) + { + result.redirect('/auth/fitbit'); + resolve(false); + } + resolve(response.body); + }); + }); +}; + +app.get('/steps', (request, result)=> +{ + queryAPI(result, 'https://api.fitbit.com/1/user/-/activities/tracker/steps/date/today/1m.json').then((data)=> + { + if(data != false) + { + result.writeHead(200, {'Content-Type': 'text/html'}); + result.write(JSON.stringify(data)); + result.end(); + } + else + { + console.log("Validating with API"); + } + }); +}); - unirest.get('https://api.fitbit.com/1/user/-/activities/steps/date/today/1m.json') - .headers({'Accept': 'application/json', 'Content-Type': 'application/json', Authorization: "Bearer " + accessTokenTemp}) - .end(function (response) +app.get('/activities', (request, result)=> +{ + queryAPI(result, 'https://api.fitbit.com/1/user/-/activities/date/1M.json').then((data)=> + { + if(data != false) { - // result.write(response.body); + result.writeHead(200, {'Content-Type': 'text/html'}); + result.write(JSON.stringify(data)); result.end(); - console.log(response.body); - }); + } + else + { + console.log("Validating with API"); + } + }); +}); + + +app.get('/', (request, result) => +{ + result.write(utils.getFile("index.html")); + result.end(); }); -app.listen(PORT, () => +app.listen(config.port, () => console.log(`App listening on port ${config.port}!`) ); \ No newline at end of file