Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
BBC-Web-Coding-Test/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
96 lines (86 sloc)
3.71 KB
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 lets me know when I'm using bad programming practices | |
//So I can correct them | |
'use strict' | |
//Libraries I will be using | |
const Koa = require('koa') | |
const Router = require('koa-router') | |
const views = require('koa-views') | |
const staticDir = require('koa-static') | |
const bodyParser = require('koa-bodyparser') | |
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) | |
const session = require('koa-session') | |
//Modules with my functions | |
const Story = require('./modules/story') | |
const Ranking = require('./modules/ranking') | |
const app = new Koa() | |
const router = new Router() | |
//Setting up my libraries | |
app.keys = ['BBCNews'] | |
app.use(staticDir('css')) | |
app.use(bodyParser()) | |
app.use(session(app)) | |
app.use(views(`${__dirname}/html`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }})) | |
const port = 8080 | |
const database = 'ratings.db' | |
//Renders Home page | |
router.get('/', async ctx => { | |
try { | |
await ctx.render('main') | |
} catch(err) { | |
console.error(err.message) | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
//Renders all the articles with their JSON data | |
//:storyNumber becomes a variable that I use to decide which JSON data to retrieve, and which page to send the user to next with the 'next' button | |
router.get('/story/:storyNumber', async ctx => { | |
try { | |
const story = await new Story("https://github.coventry.ac.uk/raw/sarkarj/BBC-Data-Copy/master/article-" + ctx.params.storyNumber + ".json", ctx.params.storyNumber) | |
const data = await story.giveData() | |
const formatted = await story.formatData(data) | |
const JSONtitle = formatted[0] | |
const JSONheader = formatted[1] | |
const JSONbody = formatted[2] | |
const nextPage = Number(ctx.params.storyNumber) + 1 | |
await ctx.render('article', {body: JSONbody, title: JSONtitle, heading: JSONheader, nextpage: nextPage}) | |
} catch(err) { | |
//The error below is the error returned when I try to render a page with JSONdata that doesn't exist | |
//This allows the program to work with any number of JSON files, as it will only send the user to the | |
//ranking page once all files from the repo have been read, and it fails to load another one. | |
if(err.message == 'Unexpected token : in JSON at position 3') await ctx.redirect('/ranking/'+ctx.params.storyNumber) | |
else await ctx.render('error', {message: err.message}) | |
} | |
}) | |
//Renders the ranking page | |
router.get('/ranking/:nOfStories', async ctx => { | |
try { | |
const ranking = await new Ranking(database) | |
const sList = await ranking.getNOfStories(ctx.params.nOfStories) | |
const NaNcheck = Number(ctx.params.nOfStories) | |
if(isNaN(NaNcheck)) throw new Error('Story index should be number') | |
await ctx.render('rank', sList) | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
//Called by the ranking page to submit the user's choices to an sql database | |
router.post('/ranking', koaBody, async ctx => { | |
try { | |
//rankBody requests the data (as an object) from the dropdown boxes in the ranking page. | |
const rankBody = ctx.request.body | |
//ranks gets the raw values of the object | |
const ranks = Object.values(rankBody) | |
const ranking = await new Ranking(database) | |
//sends rank to database | |
await ranking.rank(ranks) | |
//checks rank is recieved by database and logs to terminal | |
const confirmation = await ranking.getLastRanking() | |
console.log(confirmation) | |
await ctx.redirect('./') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
//Starts the program with the above routes | |
app.use(router.routes()) | |
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`)) |