-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
102 lines (85 loc) · 3.29 KB
/
server.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Include Server Dependencies
var express = require("express");
var bodyParser = require("body-parser");
var logger = require("morgan");
var passport = require('passport');
var session = require('express-session');
var env = require('dotenv').load();
var path = require('path');
var db = require("./models");
var cors = require('cors')
var request = require('request');
var flash = require('express-flash');
var rp = require('request-promise');
// Create a new express app
var app = express();
app.use(cors())
// Sets an initial port. We'll use this later in our listener
var PORT = process.env.PORT || 5000;
// Run Morgan for Logging
app.use(logger("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.text());
app.use(bodyParser.json({type: "application/vnd.api+json"}));
app.use(express.static(__dirname + '/dist'));
//For passport sessions
app.use(session({secret: 'randomactsofkindness'}));
app.use(passport.initialize());
app.use(passport.session()); //persistent login sessions
app.use(flash());
// -------------------------------------------------
require('./routes/express.js')(app, rp);
require('./config/passport.js')(app, passport);
// -------------------------------------------------
// * (get) - load HTML page (with ReactJS) in public/index.html. Make sure you put this after all other GET routes
//redirect the user to our rendered React application
//server-side org hunter api call by term
app.get('/api/get-organizations/:term', function (req, res) {
const API_KEY = '58c5806d11fbc6c7da9b796db4f9a77c';
const ROOT_URL = `http://data.orghunter.com/v1/charitysearch?user_key=${API_KEY}&eligible=1`;
const url = `${ROOT_URL}&searchTerm=`;
const query = req.params.term
function orgRequest(endpoint,term){
request(endpoint+term , function (error, response, body) {
res.json(response);
});
};
orgRequest(url, query);
});
//server-side org hunter api call by term
app.get('/api/get-organization/:term/:rows', function (req, res) {
const API_KEY = '58c5806d11fbc6c7da9b796db4f9a77c';
const ROOT_URL = `http://data.orghunter.com/v1/charitysearch?user_key=${API_KEY}&eligible=1 `;
const query = req.params.term
const rows = req.params.rows
function orgRequest(endpoint,term, rows){
console.log('resquesting:', `${endpoint}&searchTerm=${term}$rows=${rows}`)
request(`${endpoint}&searchTerm=${term}$rows=${rows}` , function (error, response, body) {
res.json(response);
});
};
orgRequest(ROOT_URL, query, rows);
});
//server-side org hunter api call for all orga
app.get('/api/get-organizations', function (req, res) {
const API_KEY = '58c5806d11fbc6c7da9b796db4f9a77c';
const ROOT_URL = `http://data.orghunter.com/v1/charitysearch?user_key=${API_KEY}&eligible=1`;
const query = req.params.term
function orgRequest(endpoint, term){
request(`endpoint&${term}`, function (error, response, body) {
res.json(response);
});
};
orgRequest(ROOT_URL, query);
});
app.get('*', function(request, response) {
response.sendFile(path.resolve(__dirname, 'dist', 'index.html'))
})
// -------------------------------------------------
// Starting our express server
db.sequelize.sync({force: false}).then(function() {
app.listen(PORT, function() {
console.log("App listening on PORT: " + PORT);
});
});