-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathindex.js
177 lines (157 loc) · 4.81 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//Routing Mechanism - URL Define Here.
const express = require('express');
//Object of Express
const app = express();
app.use(express.static('views'));
//File Stream to Read Quotes from file
//I am not using db faster execution :p
var fs = require('fs');
app.get('/', (req, res) => {
fs.readFile('quotes.db', function(err, buf) {
var qarr = JSON.parse(buf);
var random = Math.floor(Math.random() * (qarr.length - 1));
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
);
// Request headers you wish to allow
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type'
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
res.send(qarr[random]);
});
});
app.get('/categories', (req, res) => {
fs.readFile('quotes.db', function(err, buf) {
var query = [];
var seenNames = {};
var i=0;
var qarr = JSON.parse(buf).map(function(n) {
if(n.category in seenNames)
{
return null;
}
else
{
seenNames[n.category] = true;
query[i++]=n.category ;
return n.category;
}
});
//console.log(seenNames);
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET');
// Request headers you wish to allow
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type'
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
res.send(JSON.stringify(query));
});
});
app.get('/author', (req, res) => {
if (
Object.entries(req.query).length !== 0 &&
req.query.constructor === Object
) {
let authorName = Object.keys(req.query);
authorName = authorName.map(author => author.toLowerCase())[0];
let dbData;
let authorQuotes = [];
fs.readFile('quotes.db', function(err, buf) {
dbData = JSON.parse(buf);
for (var i = 0; i < dbData.length; i++) {
if (dbData[i]['author'].toLowerCase().indexOf(authorName) != -1) {
authorQuotes.push(dbData[i]);
}
}
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET');
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type'
);
res.setHeader('Access-Control-Allow-Credentials', true);
res.send(JSON.stringify(authorQuotes));
});
return;
}
fs.readFile('quotes.db', function(err, buf) {
var query = [];
var seenNames = {};
var i=0;
var qarr = JSON.parse(buf).map(function(n) {
if(n.author in seenNames)
{
return null;
}
else
{
seenNames[n.author] = true;
query[i++]=n.author ;
return n.author;
}
});
//console.log(seenNames);
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET');
// Request headers you wish to allow
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type'
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
res.send(JSON.stringify(query));
});
});
app.get('/:id', (req, res) => {
fs.readFile('quotes.db', function(err, buf) {
var qarr = JSON.parse(buf).filter(function(n) {
return n.category === req.params.id;
});
//console.log(qarr.length);
var random = Math.floor(Math.random() * (qarr.length));
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, OPTIONS, PUT, PATCH, DELETE'
);
// Request headers you wish to allow
res.setHeader(
'Access-Control-Allow-Headers',
'X-Requested-With,content-type'
);
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
if(qarr[random]==undefined)
{
res.send({});
}
else res.send(qarr[random]);
});
});
//Httpserver Port Number 3000.
app.listen(process.env.PORT || 3000, function() {
console.log(
'Express server listening on port %d in %s mode',
this.address().port,
app.settings.env
);
});
//Set The File Type To App As EJS.
app.set('view engine', 'ejs');