diff --git a/index.html b/index.html new file mode 100644 index 0000000..87b9e43 --- /dev/null +++ b/index.html @@ -0,0 +1,27 @@ + + + + + Random Quote API + + + +

random-quote-api

+

a simple api that returns random quotes from famous authors

+

GitHub

+

API

+

API

+
GET /api/quotes
+
+
{
+    "quotes": [
+        {
+            "author": "string",
+            "quote": "string",
+            "id": "number"
+        }
+    ]
+}
+
+ + \ No newline at end of file diff --git a/package.json b/package.json index dd5d268..a8803db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "random-quote-api", - "version": "1.2.1", + "version": "1.3.1", "description": "get random quotes", "main": "server.js", "scripts": { diff --git a/server.js b/server.js index 1a289f3..996e007 100644 --- a/server.js +++ b/server.js @@ -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'); @@ -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) { @@ -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); \ No newline at end of file