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?
foundation/exercises/06_code_quality/todo/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.
58 lines (50 sloc)
1.49 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 | |
var Koa = require("koa") | |
var Router = require("koa-router"); | |
var stat = require("koa-static"); | |
var bodyParser = require("koa-bodyparser"); | |
var handlebars = require("koa-hbs-renderer"); | |
var app = new Koa(); | |
var router = new Router(); | |
app.use(stat ("public")); | |
app.use(bodyParser()); | |
app.use(handlebars({ paths: { views: __dirname + "/views" } })); | |
app.use(router.routes()); | |
var port = 8080 | |
const List = require('./modules/list').List | |
const list = new List() | |
router.get("/", async function(ctx) { | |
try { | |
var items = [] // you will need to add a call to the 'list' object!!! | |
const items = list.getAll() | |
console.log(items) | |
var data = {items} | |
ctx.render('home', data); | |
} catch(err) { | |
console.log(err.message); | |
ctx.render('home', {msg: err.message}); | |
} | |
}); | |
router.post("/", function(ctx) { | |
try { | |
var body = ctx.request.body; | |
console.log(body) | |
// you will need to add a call to the 'list' object!!! | |
ctx.redirect("/"); | |
} catch(err) { | |
console.log(err.message); | |
ctx.redirect("/?msg=" + err.message); | |
} | |
}); | |
router.get("/delete/:key", function(ctx) { | |
try { | |
console.log(`key: ${ctx.params.key}`); | |
// you will need to add a call to the 'list' object!!! | |
ctx.redirect('/?msg=item deleted'); | |
} catch(err) { | |
console.log(err.message); | |
ctx.redirect("/" + err.message); | |
} | |
}); | |
module.exports = app.listen(port, function() { | |
}); |