From 17766d1c826130b44b2b2d788965c91d57a29b23 Mon Sep 17 00:00:00 2001 From: Luke Peavey <1williampeavey@gmail.com> Date: Tue, 18 Jun 2019 04:33:04 -0400 Subject: [PATCH] Refactor code --- package.json | 5 ++++- src/config.js | 5 ----- src/handleErrors.js | 21 +++++++++++++++++++++ src/routes.js | 20 ++++++++++++++++++++ src/server.js | 40 +++++++++++++--------------------------- 5 files changed, 58 insertions(+), 33 deletions(-) delete mode 100644 src/config.js create mode 100644 src/handleErrors.js create mode 100644 src/routes.js diff --git a/package.json b/package.json index 3e6bbe8..02d322d 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,9 @@ } }, "dependencies": { + "cors": "^2.8.5", "express": "4.17.1", + "http-errors": "^1.7.2", "jsonfile": "5.0.0", "lodash": "4.17.11" }, @@ -40,7 +42,8 @@ "jest": "^24.8.0", "lint-staged": "^8.2.1", "nodemon": "^1.19.1", - "prettier": "^1.18.2" + "prettier": "^1.18.2", + "shortid": "^2.2.14" }, "engines": { "node": "12.x" diff --git a/src/config.js b/src/config.js deleted file mode 100644 index 16aa35b..0000000 --- a/src/config.js +++ /dev/null @@ -1,5 +0,0 @@ -const config = { - PORT: process.env.PORT || 4000 -} - -module.exports = config diff --git a/src/handleErrors.js b/src/handleErrors.js new file mode 100644 index 0000000..4e3bddb --- /dev/null +++ b/src/handleErrors.js @@ -0,0 +1,21 @@ +const createError = require('http-errors') +/** Handles 404 errors */ +module.exports.handle404 = function handle404(req, res, next) { + return next(createError(404, 'The requested resource could not be found')) +} + +/** Logs errors in development mode */ +module.exports.logErrors = function logErrors(error, req, res, next) { + if (process.env.NODE_ENV === 'development') { + /* eslint-disable-next-line no-console */ + console.error(error.stack) + } + return next(error) +} + +/** Sends error response to client */ +module.exports.handleErrors = function handleErrors(error, req, res, next) { + const statusCode = error.status || 500 + const statusMessage = error.message || 'Internal server error' + res.status(statusCode).json({ statusCode, statusMessage }) +} diff --git a/src/routes.js b/src/routes.js new file mode 100644 index 0000000..3bcbe85 --- /dev/null +++ b/src/routes.js @@ -0,0 +1,20 @@ +const { Router } = require('express') +const random = require('lodash/random') +const data = require('../data/quotes.json') + +const router = Router() +const NUMBER_OF_ENTRIES = data.length + +/** + * Get a single random quote + */ +router.get('/random', function(req, res) { + const index = random(0, NUMBER_OF_ENTRIES - 1) + const entry = data[index] + if (!entry) { + throw new Error('Internal server error') + } + res.status(200).json(entry) +}) + +module.exports = router diff --git a/src/server.js b/src/server.js index e3437ca..f11e163 100644 --- a/src/server.js +++ b/src/server.js @@ -1,33 +1,19 @@ -// init project const express = require('express') -const random = require('lodash') -const data = require('../data/quotes.json') -const config = require('./config') +const cors = require('cors') +const routes = require('./routes') +const { handle404, logErrors, handleErrors } = require('./handleErrors') -const app = express() - -// static files are served from public -app.use(express.static('public')) - -app.use(function(req, res, next) { - res.header('Access-Control-Allow-Origin', '*') - res.header( - 'Access-Control-Allow-Headers', - 'Origin, X-Requested-With, Content-Type, Accept' - ) - next() -}) +const PORT = process.env.PORT || 4000 -app.get('/', function(req, res) { - res.redirect('https://github.com/lukepeavey/quota') -}) - -app.get('/random', function(req, res) { - let randomQuote = data[random(0, data.length - 1)] - if (randomQuote) res.json(randomQuote) -}) +// Create the Express server +const app = express() +app.use(cors()) +app.use(routes) +app.use(handle404) +app.use(logErrors) +app.use(handleErrors) -var listener = app.listen(config.PORT, function() { +app.listen(PORT, () => { /* eslint-disable-next-line no-console */ - console.log('Your app is listening on port ' + listener.address().port) + console.log(`Server is running on port: ' ${PORT}`) })