API reference in Node and Aurelia

Here is a comparison and a quick reference for using AJAX and POSTGRES in front-end Aurelia and back-end Node.js. I found myself searching for this code each time I wanted to access databases and make api calls. Hope it’s useful.

Aurelia

getAndDisplayTasks() {
    this.formattedDate = moment(this.currentDate, "DDMMYYYY").format("MMMM DD, YYYY");
    client.fetch(`http://localhost:3000/api/tasks/${this.currentDate}`)
        .then(response => response.json())
        .then(tasks => {
            this.tasks = tasks;
        });
}

var task = {
        task: $("#taskInput").val(),
        deadline: $("#deadlineInput").val(),
        date: moment($("#dateInput").val()).format("DDMMYYYY")
    }

client
    .fetch(`http://localhost:3000/api/tasks/`, {
        method: 'post',
        body: json(task)
    }).then(() => {
        this.getAndDisplayTasks();
    })
    .catch(error => {
        alert('Error saving comment!');
    });

updateTask(task) {
    client
        .fetch(`http://localhost:3000/api/tasks/${task.task_id}`, {
            method: 'put',
            body: json(task)
        })
        .catch(error => {
            alert('Error saving comment!');
        });
}

deleteTask(task) {
    client
        .fetch(`http://localhost:3000/api/tasks/${task.task_id}`, {
            method: 'delete',
            body: json(task)
        })
        .then(() => {
            this.getAndDisplayTasks();
        })
        .catch(error => {
            alert('Error saving comment!');
        });

}

Node JS

app.get('/api/tasks/:date', (req, res) => {
    db.query("select * from tasks")
    .then(db_tasks => {
    var date = req.params.date;
    var tasks_for_date = _.filter(db_tasks, task => task.date == date);
    res.json(tasks_for_date);
})
.catch(err => console.log(err));
})

app.post('/api/tasks', (req, res) => {
    var task = req.body.task;
    var deadline = req.body.deadline;
    var date = req.body.date;
    var query = `INSERT INTO tasks (task, deadline, date, completed) VALUES ('${task}', '${deadline}', '${date}', false)`;
    db.query(query);
    res.end();
})

app.put('/api/tasks/:taskid', (req, res) => {
    var task = req.body.task;
    var deadline = req.body.deadline;
    var date = req.body.date;
    var completed = req.body.completed;
    var query = `UPDATE tasks 
                SET task='${task}', deadline='${deadline}', date='${date}', completed='${completed}'
                WHERE task_id=${req.params.taskid}`;
    db.query(query);
    res.end();
})

app.delete('/api/tasks/:taskid', (req, res) => {
    var query = `DELETE from tasks 
            WHERE task_id=${req.params.taskid}`;
    db.query(query);
       res.end();
})