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?
TEACHING-MATERIALS/exercises/05_advanced_testing/02_acceptance/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.
62 lines (50 sloc)
1.16 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 | |
'use strict' | |
const Koa = require('koa') | |
const Router = require('koa-router') | |
const bodyParser = require('koa-bodyparser') | |
const views = require('koa-views') | |
const app = new Koa() | |
app.use(bodyParser()) | |
app.use(require('koa-static')('public')) | |
app.use(views(`${__dirname}/views`, { extension: 'handlebars' }, { | |
map: { | |
handlebars: 'handlebars' | |
} | |
})) | |
const router = new Router() | |
const port = 8080 | |
const todo = require('./modules/todo') | |
router.get('/', async ctx => { | |
try { | |
const data = todo.getAll() | |
await ctx.render('home', {items: data}) | |
} catch(err) { | |
console.error(err.message) | |
await ctx.render('empty') | |
} | |
}) | |
router.post('/', async ctx => { | |
try { | |
console.log(ctx.request.body) | |
todo.add(ctx.request.body.item, ctx.request.body.qty) | |
} catch(err) { | |
console.error(err.message) | |
} finally { | |
ctx.redirect('/') | |
} | |
}) | |
router.get('/delete/:index', async ctx => { | |
try { | |
console.log(`item: ${ctx.params.index}`) | |
todo.delete(ctx.params.index) | |
} catch(err) { | |
console.error(err.message) | |
} finally { | |
ctx.redirect('/') | |
} | |
}) | |
app.use(router.routes()) | |
app.use(router.allowedMethods()) | |
const server = app.listen(port) | |
module.exports = server |