Learn Rest API Using Express
Learn Rest API Using Express
js and MySQL
DB
Published Jul 19, 2018Last updated Jun 24, 2019
// todoList Routes
app.route('/tasks')
.get(todoList.list_all_tasks)
.post(todoList.create_a_task);
app.route('/tasks/:taskId')
.get(todoList.read_a_task)
.put(todoList.update_a_task)
.delete(todoList.delete_a_task);
};
Let’s create a db connection wrapper, this will allow you to create connection on db
which stored on a single file and can be reuse by other modules.
To do this create a new file name db.js under models folder
$ touch db.js
Paste the code snippet below:
'user strict';
connection.connect(function(err) {
if (err) throw err;
});
module.exports = connection;
Setting up model:
We can now reuse our db instance to other modules
Now create are Task object and provide function like creating new task, get all task, get
task by id, update task by id and delete task by id.
Open appModel.js and paste code snippet below:
'user strict';
var sql = require('./db.js');
if(err) {
console.log("error: ", err);
result(err, null);
}
else{
console.log(res.insertId);
result(null, res.insertId);
}
});
};
Task.getTaskById = function (taskId, result) {
sql.query("Select task from tasks where id = ? ", taskId, function
(err, res) {
if(err) {
console.log("error: ", err);
result(err, null);
}
else{
result(null, res);
}
});
};
Task.getAllTask = function (result) {
sql.query("Select * from tasks", function (err, res) {
if(err) {
console.log("error: ", err);
result(null, err);
}
else{
console.log('tasks : ', res);
result(null, res);
}
});
};
Task.updateById = function(id, task, result){
sql.query("UPDATE tasks SET task = ? WHERE id = ?", [task.task, id],
function (err, res) {
if(err) {
console.log("error: ", err);
result(null, err);
}
else{
result(null, res);
}
});
};
Task.remove = function(id, result){
sql.query("DELETE FROM tasks WHERE id = ?", [id], function (err, res) {
if(err) {
console.log("error: ", err);
result(null, err);
}
else{
result(null, res);
}
});
};
module.exports= Task;
In here we require db.js to import our connection instance to mysql db and perform
mysql queries.
Setting up the controller
Open appController.js file with your text edito and let’s deep dive into coding.
In this controller, we would be writing five(5) different functions namely: list_all_tasks,
create_a_task, read_a_task, update_a_task, delete_a_task. We will export each of the
functions for us to use in our routes.
Each of these functions uses different Task methods we created previously in
appModel.js such as getTaskById, getAllTask, updateById, createTask and remove.
Open appController.js and paste code snippet below
'use strict';
console.log('controller')
if (err)
res.send(err);
console.log('res', task);
res.send(task);
});
};
}
else{
if (err)
res.send(err);
res.json(task);
});
}
};
// connect to database
mc.connect();
app.listen(port);
2. On the same address, change the method to POST, click body and select “raw”
and on the drop
menu choose application/json.
on body
provide task details:
i.e:
{
“task”:”create repo”,
“status”:”1”
}
After this, click on send button.
This should give you a response 200 ok