Skip to content

Instantly share code, notes, and snippets.

@shalaby
Last active February 9, 2021 09:29
Show Gist options
  • Save shalaby/e1f94a7c6d42126b3ce4e3c21da6e470 to your computer and use it in GitHub Desktop.
Save shalaby/e1f94a7c6d42126b3ce4e3c21da6e470 to your computer and use it in GitHub Desktop.
Simple Todo server application.
const express = require('express')
const app = express()
let todos = []
app.get('/', function(req, res) {
const todosHtml = todos.map(function(todo, idx) {
let status = ''
if (todo.done == true) {
status = 'checked'
}
return `
<div>
<input type="checkbox" ${status} onclick="window.location.href='./completeTask?index=${idx}'">
${todo.task}
</div>`
})
res.send(`
<h1>Todo List</h1>
<form action="/add">
<input name="newTaskFromUser">
<button>Submit</button>
</form>
<p>${todosHtml.join('')}</p>
`)
})
app.get('/add', function(req, res) {
const newTask = req.query.newTaskFromUser
const newTodo = {
task: newTask,
done: false
}
todos.push(newTodo)
res.redirect('/')
})
app.get('/completeTask', function(req, res) {
const todoIndex = req.query.index
todos[todoIndex].done = !todos[todoIndex].done
res.redirect('/')
})
app.listen(3000, function() {
console.log('Server is running ...')
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment