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?
template-single-page-app/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.
101 lines (91 sloc)
2.97 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
const Koa = require('koa') | |
const Router = require('koa-router') | |
const staticDir = require('koa-static') | |
const send = require('koa-send') | |
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'}) | |
const app = new Koa() | |
const router = new Router() | |
const defaultPort = 8080 | |
const port = process.env.PORT || defaultPort | |
const dbName = 'website.db' | |
const User = require('./modules/user') | |
const List = require('./modules/list') | |
app.use(staticDir('public')) | |
router.get('/', async ctx => { | |
console.log('---------------------------------------------------------------') | |
console.log('GET /') | |
try { | |
await send(ctx, 'public/index.html') | |
} catch(err) { | |
console.log(err.message) | |
await send(ctx, 'public/index.html') | |
} | |
}) | |
router.post('/register', koaBody, async ctx => { | |
console.log('---------------------------------------------------------------') | |
console.log('POST /register') | |
const user = await new User(dbName) | |
try { | |
const body = typeof ctx.request.body === 'string' ? JSON.parse(ctx.request.body) : ctx.request.body | |
await user.register(body.user, body.pass, body.email) | |
ctx.status = 201 | |
ctx.body = {status: 'success', msg: 'account created'} | |
} catch(err) { | |
ctx.status = 422 | |
ctx.body = {status: "error", msg: err.message} | |
} finally { | |
user.tearDown() | |
} | |
}) | |
router.get('/login', async ctx => { | |
console.log('---------------------------------------------------------------') | |
console.log('GET /login') | |
const user = await new User(dbName) | |
try { | |
const header = ctx.request.headers.authorization | |
const hash = header.split(' ')[1] | |
const status = await user.login(hash) | |
ctx.status = 200 | |
ctx.body = {status: 'success', msg: 'valid credentials'} | |
} catch(err) { | |
ctx.status = 401 | |
ctx.body = {status: "error", msg: err.message} | |
} finally { | |
user.tearDown() | |
} | |
}) | |
router.post('/lists', koaBody, async ctx => { | |
console.log('---------------------------------------------------------------') | |
console.log('POST /lists') | |
const list = await new List(dbName) | |
try { | |
const body = typeof ctx.request.body === 'string' ? JSON.parse(ctx.request.body) : ctx.request.body | |
console.log(body) | |
const header = ctx.request.headers.authorization | |
console.log(header) | |
body.id = await list.add(header, body.listname, body.description) | |
ctx.status = 201 | |
ctx.body = {status: 'success', msg: 'list added', data: ctx.body} | |
} catch(err) { | |
ctx.status = 422 | |
ctx.body = {status: "error", msg: err.message} | |
} finally { | |
list.tearDown() | |
} | |
}) | |
router.get('/lists', async ctx => { | |
console.log('---------------------------------------------------------------') | |
console.log('GET /lists') | |
const list = await new List(dbName) | |
try { | |
const lists = await list.getLists(ctx.request.headers.authorization) | |
console.log(lists) | |
ctx.status = 200 | |
ctx.body = {status: 'success', msg: 'lists found', data: lists} | |
} catch(err) { | |
ctx.status = 422 | |
ctx.body = {status: "error", msg: err.message} | |
} | |
}) | |
app.use(router.routes()) | |
module.exports = app.listen(port, () => console.log(`listening on port ${port}`)) |