Skip to content
Permalink
master
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env node
//Routes File
'use strict'
/* MODULE IMPORTS */
//const bcrypt = require('bcrypt-promise')
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 sqlite = require('sqlite-async')
const fs = require('fs')
//const mime = require('mime-types')
const multer = require('@koa/multer')
//const jimp = require('jimp')
/* IMPORT CUSTOM MODULES */
const User = require('./modules/user')
const Forum = require('./modules/forum')
//const Home = require('./modules/home')
const UpdateForum = require('./modules/update')
const app = new Koa()
const router = new Router()
const upload = multer()
/* 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 = 'website.db'
//const saltRounds = 10
/*sqlite.open(dbName)
.then(db => {
console.log('DB connected')
})
.catch(err => {
console.log('DB err', err)
})*/
/**
* The secure home page.
*
* @name Home Page
* @route {GET} /
* @authentication This route requires cookie-based authentication.
*/
router.get('/', async ctx => {
try {
if (ctx.session.authorised !== true) return ctx.redirect('/login')
const data = {}
if (ctx.query.msg) data.msg = ctx.query.msg
await ctx.redirect('/home')
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
/**
* The main display page.
*
* @name Forum Page
* @route {GET} /main
*/
router.get('/main', async ctx => await ctx.render('main'))
/**
* The user forum display page.
*
* @name Forum Page
* @route {GET} /forum
*/
router.get('/forum', async ctx => await ctx.render('forum'))
/**
* The home display page.
*
* @name Forum Page
* @route {GET} /home
*/
router.get('/home', async ctx => {
try {
const forum = await new Forum(dbName)
const forumData = await forum.loadForumsData()
await ctx.render('home', { forumData })
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
/**
* The script to process forum creation.
*
* @name Register Script
* @route {POST} /forum
*/
router.post('/forum', koaBody, async ctx => {
try {
// extract the data from the request
const body = ctx.request.body
// call the functions in the module
const forum = await new Forum(dbName)
const user = await new User(dbName)
const selectedUser = await user.getUserDetails(ctx.session.userData)
const update = await new UpdateForum(dbName)
const data= await forum.registerForum(body, selectedUser)
data.forum_id = data.lastID
//console.log('ctx.session.userData', ctx.session.userData);console.log('data', data);
const messageData = await update.loadMessageData(data)
const forumData = await forum.loadForumData(data)
// redirect to the newly created forum page
await ctx.render('update', { forumData: forumData, messageData: messageData })
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
router.get('/update', async ctx => {
await ctx.render('update')
})
router.post('/update-admin', koaBody, async ctx => {
try {
const body = ctx.request.body
//console.log('body----', body)
const update = await new UpdateForum(dbName)
const user = await new User(dbName)
const selectedUser = await user.getUserDetails(ctx.session.userData)
const messageData = {message_id: body.message_id,forum_id: body.forum_id,visibility: body.visibility}
//const data = await update.updateMessage(messageData, selectedUser)
await update.updateMessage(messageData, selectedUser)
//console.log(data)
await ctx.redirect('/home')
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
/**
* The script to process replies.
*
* @name Update Script
* @route {POST} /update
*/
router.post('/update', koaBody, async ctx => {
try {
const body = ctx.request.body
// call the functions in the module
const update = await new UpdateForum(dbName)
const forum = await new Forum(dbName)
//console.log('ctx.session.userData', ctx.session.userData)
const messageData = await update.loadMessageData(body)
const forumData = await forum.loadForumData(body)
// redirect to the particular forum page
await ctx.render('update', { forumData: forumData, messageData: messageData })
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
router.post('/update-comment', koaBody, async ctx => {
try {
const body = ctx.request.body
// call the functions in the module
const update = await new UpdateForum(dbName)
const forum = await new Forum(dbName)
const user = await new User(dbName)
const selectedUser = await user.getUserDetails(ctx.session.userData)
await update.createMessage(body, selectedUser)
const messageData = await update.loadMessageData(body)
const forumData = await forum.loadForumData(body)
// redirect to the particular forum page
await ctx.render('update', { forumData: forumData, messageData: messageData })
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
/**
* The user registration page.
*
* @name Register Page
* @route {GET} /register
*/
router.get('/register', async ctx => await ctx.render('register'))
/**
* The script to process new user registrations.
*
* @name Register Script
* @route {POST} /register
*/
router.post('/register', upload.single('avatar'), async ctx => {
try {
const body = ctx.request.body
//console.log(body)
const user = await new User(dbName)
const path='./public/avatars/'
if (ctx.file) {
const filename = ctx.file.originalname
const filepath = path + filename
fs.writeFile(filepath, ctx.file.buffer, async() => {
/*if (err) {
return console.log(err, 'dpppp')
}*/
await user.register(body.user, body.pass, ctx.file.originalname)
})
} else {
await user.register(body.user, body.pass, '')
}
ctx.redirect(`/?msg=new user "${body.name}" 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('login', data)
})
router.post('/login', async ctx => {
try {
const body = ctx.request.body
const user = await new User(dbName)
const data =await user.login(body.user, body.pass)
ctx.session.authorised = true
ctx.session.userData = data
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.post('/search', koaBody, async ctx => {
try {
const body = ctx.request.body
//console.log(body, 'ddddd')
const forum = await new Forum(dbName)
const forumData = await forum.searchForumsData(body.searchText)
await ctx.render('home', { forumData })
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
router.post('/message-search', koaBody, async ctx => {
try {
const body = ctx.request.body
//console.log(body, 'ddddd')
const update = await new UpdateForum(dbName)
const forum = await new Forum(dbName)
const forumData = await forum.loadForumData(body)
const messageData = await update.searchMessagesData(body.searchText)
await ctx.render('update', { forumData: forumData, messageData: messageData })
} catch (err) {
await ctx.render('error', { message: err.message })
}
})
/**
* The Admin Message Visibility Update Page.
*
* @name Ad Page
* @route {GET} /admin_update
*/
router.get('/admin_update', async ctx => await ctx.render('admin_update'))
router.post('/admin_update', koaBody, async ctx => {
try {
const body = ctx.request.body
//console.log(body,ctx.session.userData)
// call the functions in the module
const update = await new UpdateForum(dbName)
const forum=await new Forum(dbName)
//console.log(ctx.session.userData)
await update.updateMessage(body, ctx.session.userData)
const messageData = await update.loadMessageData(body, ctx.session.userData)
const forumData = await forum.loadForumData(body)
// redirect to the particular forum pages
await ctx.render('update', { forumData: forumData, messageData: messageData })
//await ctx.render('home',{forumData})
} 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}`))