Skip to content
Permalink
9699377779
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
52 lines (42 sloc) 1.07 KB
#!/usr/bin/env node
'use strict'
const express = require('express')
const chalk = require('chalk')
const handlebars = require('express-handlebars').create({defaultLayout: 'main'})
const bodyParser = require('body-parser')
const app = express()
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: true }))
app.engine('handlebars', handlebars.engine)
app.set('view engine', 'handlebars')
const port = 8080
const todo = require('./modules/todo')
app.get('/', (req, res) => {
try {
const data = todo.getAll()
res.render('home', {items: data})
} catch(err) {
console.error(err.message)
res.render('empty')
}
})
app.post('/', (req, res) => {
try {
todo.add(req.body.item, req.body.qty)
} catch(err) {
console.error(err.message)
} finally {
res.redirect('/')
}
})
app.get('/delete/:key', (req, res) => {
try {
console.log(`key: ${req.params.key}`)
todo.delete(req.params.key)
} catch(err) {
console.error(err.message)
} finally {
res.redirect('/')
}
})
app.listen(port, () => console.log(chalk.green(`app listening on port ${port}`)))