Skip to content

Commit

Permalink
Added Option to Search for Particular Author
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekAppscrip committed Oct 3, 2019
1 parent fd33697 commit ac03166
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 25 deletions.
93 changes: 69 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,75 +1,120 @@
//Routing Mechanism - URL Define Here.
const express = require("express");
const express = require('express');

//Object of Express
const app = express();
app.use(express.static("views"));
app.use(express.static('views'));

//File Stream to Read Quotes from file
//I am not using db faster execution :p
var fs = require("fs");
var fs = require('fs');

app.get("/", (req, res) => {
fs.readFile("quotes.db", function(err, buf) {
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", "*");
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
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");
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.setHeader('Access-Control-Allow-Credentials', true);
res.send(qarr[random]);
});
});

app.get("/got", (req, res) => {
fs.readFile("quotes.db", function(err, buf) {
app.get('/got', (req, res) => {
fs.readFile('quotes.db', function(err, buf) {
var qarr = JSON.parse(buf).filter(function(n) {
return n.category === "got";
return n.category === 'got';
});
var random = Math.floor(Math.random() * (qarr.length - 1));
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
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");
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.setHeader('Access-Control-Allow-Credentials', true);
res.send(qarr[random]);
});
});

app.get("/author", (req, res) => {
fs.readFile("quotes.db", function(err, buf) {
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 qarr = JSON.parse(buf).map(function(n) {
return n.author;
});
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader("Access-Control-Allow-Methods", "GET");
res.setHeader('Access-Control-Allow-Methods', 'GET');

// Request headers you wish to allow
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With,content-type");
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.setHeader('Access-Control-Allow-Credentials', true);
res.send(JSON.stringify(qarr));
});
});

//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);
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");
app.set('view engine', 'ejs');
2 changes: 1 addition & 1 deletion quotes.db
Original file line number Diff line number Diff line change
Expand Up @@ -22059,5 +22059,5 @@
{
"quote": "I'm not obsessed with sex, I just can't stop thinking about it. The performance of it. The awkwardness of it. The drama of it. The moment you realise someone wants your body. Not so much the feeling of it.",
"author": "Fleabag"
},
}
]

0 comments on commit ac03166

Please sign in to comment.