-
-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32747a6
commit 17766d1
Showing
5 changed files
with
58 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`) | ||
}) |