forked from reisborw/FAQ
Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Formatted code for new index.js
Code now works with Index.js Created new file questionRoutes.js to handle question routes
- Loading branch information
Showing
7 changed files
with
83 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"files.eol": "\n" | ||
} |
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 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,44 +0,0 @@ | ||
'use strict' | ||
|
||
const Router = require('koa-router') | ||
const Question = require('./models/question') | ||
|
||
//temp | ||
const Database = require('sqlite-async') | ||
const handlebars = require('koa-hbs-renderer') | ||
const dbName = 'server.db' | ||
// | ||
|
||
const router = new Router() | ||
const question = new Question() | ||
|
||
/* | ||
router.get('/', async ctx => await ctx.render('home', {title: 'Home'})) | ||
*/ | ||
|
||
//temp | ||
router.get('/', async ctx => { | ||
const data = await question.getAllQuestions(ctx.query) | ||
console.log(data) | ||
await ctx.render('home', {Questions: data,title: 'Home'}) | ||
}) | ||
|
||
router.post('/insertquestion', async ctx => { | ||
try { | ||
console.log(ctx.request.body) | ||
const body = ctx.request.body | ||
const sql = `INSERT INTO Questions(title, question) | ||
VALUES("${body.title}", "${body.question}");` | ||
console.log(sql) | ||
const db = await Database.open(dbName) | ||
await db.run(sql) | ||
await db.close() | ||
ctx.redirect('/') | ||
} catch(err) { | ||
ctx.body = err.message | ||
} | ||
}) | ||
|
||
router.get('/createquestion', async ctx => ctx.render('createquestion')) | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict' | ||
|
||
const Router = require('koa-router') | ||
const Question = require('../models/question') | ||
|
||
const router = new Router() | ||
const question = new Question() | ||
|
||
|
||
router.get('/', async ctx => { | ||
try{ | ||
const data = await question.getAllQuestions(ctx.query) | ||
console.log(data) | ||
await ctx.render('home', {Questions: data,title: 'Home'}) | ||
}catch(err) { | ||
await ctx.render('error', {message: err.message}) | ||
} | ||
}) | ||
|
||
router.post('/insertquestion', async ctx => { | ||
try{ | ||
await question.insertQuestion(ctx.request) | ||
ctx.redirect('/') | ||
}catch(err) { | ||
await ctx.render('error', {message: err.message}) | ||
} | ||
}) | ||
|
||
router.get('/createquestion', async ctx => ctx.render('createquestion')) | ||
|
||
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
|
||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>ERROR</title> | ||
</head> | ||
<body> | ||
<h1>An Error Has Occurred</h1> | ||
<h2>{{message}}</h2> | ||
</body> | ||
</html> |
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,6 @@ | ||
'use strict' | ||
|
||
require('dotenv').config() | ||
const app = require('./server') | ||
|
||
app.listen(process.env.SERVER_PORT, () => console.log(`Server running on ${process.env.SERVER_PORT}...`)) |
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