Skip to content

Commit

Permalink
create main page and 404 error endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pbgnz committed Mar 31, 2021
1 parent 14defd4 commit d5122ca
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
27 changes: 27 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Random Quote API</title>
<meta name="description" content="Random Quote API">
<meta name="author" content="pbgnz">
</head>
<h1 id="random-quote-api">random-quote-api</h1>
<p>a simple api that returns random quotes from famous authors </p>
<p><a href="https://github.com/pbgnz/random-quote-api" target="_blank">GitHub</a></p>
<p><a href="https://pbgnz-random-quotes-api.herokuapp.com/api/quotes" target="_blank">API</a></p>
<h2 id="api">API</h2>
<pre><code class="lang-bash">GET <span class="hljs-regexp">/api/</span>quotes
</code></pre>
<pre><code class="lang-bash">{
<span class="hljs-attr">"quotes"</span>: [
{
<span class="hljs-attr">"author"</span>: <span class="hljs-string">"string"</span>,
<span class="hljs-attr">"quote"</span>: <span class="hljs-string">"string"</span>,
<span class="hljs-attr">"id"</span>: <span class="hljs-string">"number"</span>
}
]
}
</code></pre>
<body></body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "random-quote-api",
"version": "1.2.1",
"version": "1.3.1",
"description": "get random quotes",
"main": "server.js",
"scripts": {
Expand Down
13 changes: 11 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require('express');
const router = express.Router();
const app = express();
const path = require('path');
const request = require('request');
const cheerio = require('cheerio');

Expand All @@ -11,12 +12,16 @@ app.use(router);

let identityMap = new Map();

router.get('/', (req, res) => {
res.status(200).sendFile(path.join(__dirname + '/index.html'));
});

router.get('/api/quotes', (req, res) => {
let pageNumber = Math.floor(Math.random() * 100) + 1;

if (identityMap.has(pageNumber)) {
const quotes = identityMap.get(pageNumber);
res.send({ quotes });
res.status(200).send({ quotes });
} else {
let quotes = [];
request('https://www.goodreads.com/quotes?page=' + pageNumber, function (err, r, body) {
Expand All @@ -33,11 +38,15 @@ router.get('/api/quotes', (req, res) => {
quotes[index].author = $(this)[0].children[0].data.replace(/(\r\n|\n|\r| +(?= )|,)/gm, "");
});
identityMap.set(pageNumber, quotes);
res.send({ quotes });
res.status(200).send({ quotes });
}
});
}
});

router.get('*', (req, res) => {
res.status(404).send({ error: "404 not found"});
});

app.listen(port);
console.log('Server is running on port ' + port);

0 comments on commit d5122ca

Please sign in to comment.