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?
WebsiteProject/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.
251 lines (225 sloc)
6.96 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
#!/usr/bin/env node | |
//Routes File | |
'use strict' | |
/* MODULE IMPORTS */ | |
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') | |
//const jimp = require('jimp') | |
/* IMPORT CUSTOM MODULES */ | |
const User = require('./modules/user') | |
const Article = require('./modules/article') | |
const Image = require('./modules/image') | |
const ifs = require('./modules/imagefs') | |
const app = new Koa() | |
const router = new Router() | |
/* CONFIGURING THE MIDDLEWARE */ | |
app.keys = ['darkSecret'] | |
app.use(staticDir('public')) | |
app.use(bodyParser()) | |
app.use(session(app)) | |
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }})) | |
const defaultPort = 8080 | |
const port = process.env.PORT || defaultPort | |
const dbName = 'database.db' | |
router.get('/', async ctx => { | |
try { | |
const article = await new Article(dbName) | |
const data = await article.search(ctx.query) | |
const numOfResults = data.length | |
if(ctx.session.authorised === true) { | |
const loggedIn = true | |
const user = await new User(dbName) | |
const userData = await user.getUser(ctx.session.user) | |
await ctx.render('homepage', {articles: data, user: userData, query: ctx.query.q, num: numOfResults, loggedIn: loggedIn}) | |
} else await ctx.render('homepage', {articles: data, query: ctx.query.q, num: numOfResults}) | |
} catch(err) { | |
console.error(err.message) | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/articles/:id', async ctx => { | |
try { | |
const id = ctx.params.id | |
const article = await new Article(dbName) | |
const data = await article.get(id) | |
const loggedIn = true | |
if(ctx.session.authorised) await ctx.render('articlepage', {article: data, loggedIn: loggedIn}) | |
else await ctx.render('articlepage', data) | |
} catch(err) { | |
ctx.body = err.message | |
console.log(err) | |
} | |
}) | |
router.get('/edit/:id', async ctx => { | |
try { | |
const id = ctx.params.id | |
const article = await new Article(dbName) | |
const data = await article.get(id) | |
await ctx.render('editarticle', data) | |
} catch(err) { | |
ctx.body = err.message | |
console.log(err) | |
} | |
}) | |
router.post('/edit', koaBody, async ctx => { | |
try { | |
const body = ctx.request.body | |
const id = body.id | |
console.log(id) | |
console.log(body) | |
const article = await new Article(dbName) | |
await article.reupload(id, body.title, body.summary, body.content, body.tag) | |
const {path, type} = ctx.request.files.image | |
const image = await new Image(dbName) | |
await ifs.deletePicture(`public/articleImages/${id}.png`) | |
await ifs.uploadPicture(path, type, `${id}`, 'public/articleImages/' ) | |
await image.replace(`public/articleImages/${id}.png`) | |
ctx.redirect(`/?msg=new "${body.title}" uploaded`) | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/categories/:tag', async ctx => { | |
try { | |
const tag = ctx.params.tag | |
const article = await new Article(dbName) | |
const data = await article.getByTag(tag) | |
await ctx.render('homepage', {articles: data}) | |
} catch(err) { | |
ctx.body = err.message | |
} | |
}) | |
/** | |
* The user registration page. | |
* | |
* @name Register Page | |
* @route {GET} /register | |
*/ | |
router.get('/register', async ctx => { | |
const data = {} | |
if(ctx.query.msg) data.msg = ctx.query.msg | |
await ctx.render('loginpage') | |
}) | |
/** | |
* The script to process new user registrations. | |
* | |
* @name Register Script | |
* @route {POST} /register | |
*/ | |
router.post('/register', koaBody, async ctx => { | |
try { | |
// extract the data from the request | |
const body = ctx.request.body | |
console.log(body) | |
// call the functions in the module | |
const user = await new User(dbName) | |
await user.register(body.user, body.pass) | |
await user.checkRights(body.user, body.rights) | |
// redirect to the home page | |
ctx.redirect(`/?msg=new user ${body.user} added`) | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/login', async ctx => { | |
const data = {} | |
if(ctx.query.msg) data.msg = ctx.query.msg | |
if(ctx.query.user) data.user = ctx.query.user | |
await ctx.render('loginpage', data) | |
}) | |
router.post('/login', async ctx => { | |
try { | |
const body = ctx.request.body | |
const user = await new User(dbName) | |
await user.login(body.user, body.pass) | |
ctx.session.authorised = true | |
ctx.session.user = body.user | |
const currentUser = await user.getUser(body.user) | |
ctx.session.id = currentUser['id'] | |
ctx.session.type = currentUser['type'] | |
console.log(ctx.session.type) | |
return ctx.redirect('/?msg=you are now logged in...') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/logout', async ctx => { | |
ctx.session.authorised = null | |
ctx.redirect('/?msg=you are now logged out') | |
}) | |
router.get('/account', async ctx => { | |
const user = await new User(dbName) | |
if(ctx.session.authorised === true) { | |
const data = await user.getUser(ctx.session.user) | |
console.log(data) | |
await ctx.render('accountpage', {account: data}) | |
} else await ctx.render('accountpage') | |
}) | |
router.post('/account', koaBody, async ctx => { | |
try{ | |
const body = ctx.request.body | |
console.log(body) | |
const user = await new User(dbName) | |
if(body.email !== undefined) await user.addEmail(ctx.session.user, body.email) | |
if(body.location !== undefined) await user.addLocation(ctx.session.user, body.location) | |
ctx.redirect('/account') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/upload', async ctx => await ctx.render('articleUpload')) | |
router.post('/upload', koaBody, async ctx => { | |
try { | |
const body = ctx.request.body | |
console.log(body) | |
const article = await new Article(dbName) | |
const id = await article.upload(body.title, body.summary, body.content, body.tag, ctx.session.user) | |
console.log(id) | |
const {path, type} = ctx.request.files.image | |
const image = await new Image(dbName) | |
await ifs.uploadPicture(path, type, `${id}`, 'public/articleImages/' ) | |
await image.add(`public/articleImages/${id}.png`) | |
ctx.redirect(`/?msg=new "${body.title}" uploaded`) | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.get('/admin', async ctx => { | |
try { | |
const article = await new Article(dbName) | |
const data = await article.getUnreleased() | |
await ctx.render('adminHome', {articles: data}) | |
} catch(err) { | |
console.error(err.message) | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
router.post('/release', koaBody, async ctx => { | |
// | |
// get released value from db | |
// change released value to 1 | |
// | |
try{ | |
const body = ctx.request.body | |
console.log(body) | |
const article = await new Article(dbName) | |
await article.release(body.release) | |
ctx.redirect(`/?msg=new "${body.release}" released`) | |
} catch(err) { | |
throw err | |
} | |
}) | |
router.post('/login', async ctx => { | |
try { | |
return ctx.redirect('/upload') | |
} catch(err) { | |
await ctx.render('error', {message: err.message}) | |
} | |
}) | |
app.use(router.routes()) | |
module.exports = app.listen(port, async() => console.log(`listening on port ${port}`)) |