From 70e9222adfa4b4b350082f65ca079fd480538836 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 16 Feb 2019 15:09:32 -0500 Subject: [PATCH] Got github's api to work by passing it through out backend. --- configManager.js | 5 +++ package.json | 3 +- public/js/githubAPI.js | 2 +- routes/api.js | 70 ++++++++++++++++++++++++++++++++++++++++++ routes/index.js | 17 ++++++++++ server.js | 3 +- 6 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 routes/api.js create mode 100644 routes/index.js diff --git a/configManager.js b/configManager.js index 3e1e451..c77a328 100644 --- a/configManager.js +++ b/configManager.js @@ -33,6 +33,11 @@ module.exports= getSessionSecret: function() { return config.sessionSecret; + }, + + getAPIUser: function() + { + return config.user; } }; \ No newline at end of file diff --git a/package.json b/package.json index 3071d92..63f7cbb 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "dependencies": { "express": "^4.16.4", "express-session": "^1.15.6", - "fs": "0.0.1-security" + "fs": "0.0.1-security", + "got": "^9.6.0" } } diff --git a/public/js/githubAPI.js b/public/js/githubAPI.js index 94ed38b..6cf8f8b 100644 --- a/public/js/githubAPI.js +++ b/public/js/githubAPI.js @@ -7,7 +7,7 @@ */ -const APIROOT = "https://api.github.com"; +const APIROOT = "api"; const API_USER_PATH = "/users/"; diff --git a/routes/api.js b/routes/api.js new file mode 100644 index 0000000..2f5bb4b --- /dev/null +++ b/routes/api.js @@ -0,0 +1,70 @@ +const routes = require('express').Router(); + + +const got = require("got"); + + +const GITHUB_API = "https://api.github.com"; + + +const configLoader = require('../configManager'); + +const authenticate = "?client_id=" + configLoader.getClientID() + + "&client_secret=" + configLoader.getClientSecret(); + + +function queryGitHubAPI(requestURL) +{ + return new Promise(function(reject, resolve) + { + const queryRUL = GITHUB_API + requestURL + authenticate; + + got(queryRUL, { json: true }).then(response => + { + resolve(response.body); + }).catch(error => + { + resolve(response.body) + }); + }) +} + +//https://api.github.com/users/whatever?client_id=xxxx&client_secret=yyyy +// function authenticateWithGitHub() +// { +// const authURL = GITHUB_API + "/users/" + configLoader.getAPIUser() + "?client_id=" + configLoader.getClientID() + +// "&client_secret=" + configLoader.getClientSecret(); +// +// return new Promise(function(resolve, reject) +// { +// got(authURL, { json: true }).then(response => +// { +// console.log(response); +// resolve(response); +// }).catch(error => { +// reject(error); +// console.log(error.response.body); +// }); +// }) +// +// } + + + + +routes.get('/*', (request, result) => +{ + const gitHubAPIURL = request.url; + + queryGitHubAPI(gitHubAPIURL).then(function(data) + { + result.write(JSON.stringify(data)); + result.end(); + }).catch(function(error) + { + result.write(JSON.stringify(error)); + result.end(); + }) +}); + +module.exports = routes; \ No newline at end of file diff --git a/routes/index.js b/routes/index.js new file mode 100644 index 0000000..e1bff90 --- /dev/null +++ b/routes/index.js @@ -0,0 +1,17 @@ +const routes = require('express').Router(); + + +const api = require('./api'); +routes.use('/api', api); + +routes.get("/", (request, result) => +{ + result.redirect("index.html"); +}); + +routes.get('*', (request, result) => +{ + result.redirect("404.html"); +}); + +module.exports = routes; \ No newline at end of file diff --git a/server.js b/server.js index 084eec9..f746243 100644 --- a/server.js +++ b/server.js @@ -21,12 +21,13 @@ app.use(express.json()); app.use(express.static('public')); - const routes = require('./routes'); app.use('/', routes); + + app.listen(configLoader.getConfiguration().port, () => console.log(`App listening on port ${configLoader.getPort()}!`) ); \ No newline at end of file