-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
139 lines (125 loc) · 4 KB
/
index.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const express = require('express');
const mongoose = require('mongoose');
/**
* Backend Server
*
* Uses express for the backend server and
* mongoose to connect to our mongoDB Atlas server
*
* Dependencies: express, mongoose
*/
/**
* Create a router for the express server. The router will hold routes that we can call
* on our server to return responses (res)
*/
const router = express.Router();
/**
* Connect to our mongoDB server given the connection string through the website.
* Only whitelisted users can access the database
* Display a message on success/failure in console
*/
mongoose.connect('mongodb+srv://admin:[email protected]/Hackiea', { useNewUrlParser: true }).then(
() => { console.log('Database is connected') },
err => { console.log('Can not connect to the database' + err) }
);
let buzzwords, apptypes, descriptions, subjects;
//Create an express application
const app = express();
//Create a local host port string (would be different on a hosted server)
const PORT = process.env.PORT || 5000;
// Use mongoose to load all of the models from mongoDB server
const buzz = mongoose.model('Buzzword', new mongoose.Schema({ text: String }), 'Buzzword').find({}, function (err, docs) {
buzzwords = docs;
});
const apptype = mongoose.model('App Type', new mongoose.Schema({ text: String }), 'App Type').find({}, function (err, docs) {
apptypes = docs;
console.log(apptypes);
})
const desc = mongoose.model('Description', new mongoose.Schema({ text: String }), 'Description').find({}, function (err, docs) {
descriptions = docs;
});
const subject = mongoose.model('Subject', new mongoose.Schema({ text: String }), 'Subject').find({}, function (err, docs) {
subjects = docs;
});
// Listen in on the PORT object, api calls can now be called
app.listen(PORT, () => {
console.log(`Server is running on PORT ${PORT}`);
});
/**
* localhost:5000/api/
*
* Test function to say hello
*/
router.get('/', function (req, res) {
res.send({ express: "hello" });
});
router.get('/hello', function (req,res){
res.send({express: "hello again!"});
});
/**
* localhost:5000/api/buzzwords
*
* Creating an api function to get all the database entries from collections
*/
router.get('/buzzwords', function (req, res) {
/**
* Buzz is our buzzwords model from the database.
* res.json sends a json object which contains an array named 'express'
*
* 'express' is the name of our entire buzzword array
*/
res.json({ buzzArray: buzzwords });
}
);
/**
* localhost:5000/api/apptypes
*
* Creating an api function to get all the database entries from collections
*/
router.get('/apptypes', function (req, res) {
/**
* apptype is our apptypes model from the database.
* res.json sends a json object which contains an array named 'express'
*
* 'express' is the name of our entire buzzword array
*/
//console.log(apptypes);
res.json({ appArray: apptypes });
});
/**
* localhost:5000/api/descriptions
*
* Creating an api function to get all the database entries from collections
*/
router.get('/descriptions', function (req, res) {
/**
* desc is our descriptions model from the database.
* res.json sends a json object which contains an array named 'express'
*
* 'express' is the name of our entire buzzword array
*/
//console.log(descriptions);
res.json({ descArray: descriptions });
});
/**
* localhost:5000/api/subjects
*
* Creating an api function to get all the database entries from collections
*/
router.get('/subjects', function (req, res) {
/**
* subject is our subjects model from the database.
* res.json sends a json object which contains an array named 'express'
*
* 'express' is the name of our entire buzzword array
*/
//console.log(subjects);
res.json({ subjArray: subjects });
});
/**
* Add the router routes to the express server
*
* The 'api' tag will make sure these routes will be called using '/api/...'
* We can add a different router later if other backend functions are needed
*/
app.use('/api', router)