--- tags: devtools2022 --- # REST-API Demo This demo is tested to work on Ubuntu 22. Original tutorial can be found [here](https://www.bezkoder.com/node-js-rest-api-express-mysql/). Clone the repository: ```bash git clone https://github.com/natalieagus/devtools-rest ``` You need to have `mysql`, `node`, and `npm` **installed**. ```bash sudo apt update sudo apt upgrade sudo apt install npm sudo apt install nodejs sudo apt install mysql-server mysql --version # check installation ``` ## `mysql` setup Start `mysql` service and login as root for the first time: ```bash sudo service mysql start sudo mysql -u root # no passwd by default ``` Ensure that mysql is running: ```bash sudo systemctl status mysql ``` In mysql, set root **password**: ```bash ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '[YOUR_SQL_PASSWORD]'; exit ``` Then start **secure** installation and **follow** the instructions. You will need to enter the password `[YOUR_SQL_PASSWORD]` you set in the previous step. > It will then ask you if you'd like to set a **new** password. It's up to you to keep the `[YOUR_SQL_PASSWORD]` or a set a new one. We recommend you keep it the same for simplicity sake. ```bash sudo mysql_secure_installation ``` Now login to your database (and key in the prompted password `YOUR_SQL_PASSWORD`): ```bash mysql -u root -p ``` Then create the database `rest_api_js` required for this demo: ```bash CREATE DATABASE rest_api_js; USE rest_api_js; CREATE TABLE IF NOT EXISTS `tutorials` ( id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, title varchar(255) NOT NULL, description varchar(255), published BOOLEAN DEFAULT false ) ENGINE=InnoDB DEFAULT CHARSET=utf8; exit ``` ## Install `node` packages Then install node packages, type the following command in the directory where `package.json` is: ```bash npm install ``` ## Run the REST Server Now open `/app/config/db.config.js` and write your password there: ```bash module.exports = { HOST: "localhost", USER: "root", PASSWORD: "[YOUR_SQL_PASSWORD]", DB: "rest_api_js", }; ``` Finally, run the server, ```bash node server.js ``` ## API Calls ### Simple `GET` Simple hello welcome message. ```bash curl -X GET \ '54.169.155.69:8080/' \ --header 'Accept: */*' ``` ![](https://i.imgur.com/cgJscRt.png) ### `GET` from `api/tutorials` Get a list of `tutorials` from the server. ```bash curl -X GET \ '54.169.155.69:8080/api/tutorials' \ --header 'Accept: */*' ``` ![](https://i.imgur.com/WDrqt1x.png) ### `POST` to `api/tutorials` Post a new tutorial to the server's database: ```bash= curl -X POST \ '54.169.155.69:8080/api/tutorials' \ --header 'Accept: */*' \ --header 'Content-Type: application/json' \ --data-raw '{ "title": "nodejs rest api", "description": "some description lorem ipsum" }' ``` ![](https://i.imgur.com/9KK6wMI.png) ### `GET` by `id` ```bash= curl -X GET \ '54.169.155.69:8080/api/tutorials/1' \ --header 'Accept: */*' ``` ![](https://i.imgur.com/jt1oW1w.png) ### `GET` by `title` ```bash curl -X GET \ '54.169.155.69:8080/api/tutorials?title=node' \ --header 'Accept: */*' ``` ![](https://i.imgur.com/dfbFdx1.png) ### `DELETE` by `id` ```bash curl -X DELETE \ '54.169.155.69:8080/api/tutorials/1' \ --header 'Accept: */*' ``` ![](https://i.imgur.com/RM0nTmW.png) ### `PUT` by `id` Updates an entry: ```bash curl -X PUT \ '154.169.155.69:8080/api/tutorials/3' \ --header 'Accept: */*' \ --header 'Content-Type: application/json' \ --data-raw '{ "title": "python rest API", "description": "some description lorem ipsum UPDATE" }' ``` ![](https://i.imgur.com/gC2zkFU.png) ### Routes The following table shows overview of the Rest APIs that are **supported**: | METHOD | URL | Action | | ---- | ---- | ---- | | GET | api/tutorials | get all Tutorials | | GET | api/tutorials/:id | get Tutorial by id | | POST | api/tutorials | add new Tutorial | | PUT | api/tutorials/:id | update Tutorial by id | | DELETE | api/tutorials/:id | remove Tutorial by id | | DELETE | api/tutorials | remove all Tutorials | | GET | api/tutorials/published | find all published Tutorials | | GET | api/tutorials?title=[kw] | find all Tutorials which title contains 'kw' | You can use [postman](https://www.postman.com) to make these calls more easily. The implementation of these routes can be seen at `/app/routes/tutorials.routes.js` ```javascript module.exports = app => { const tutorials = require("../controllers/tutorial.controller.js"); var router = require("express").Router(); // Create a new Tutorial router.post("/", tutorials.create); // Retrieve all Tutorials router.get("/", tutorials.findAll); // Retrieve all published Tutorials router.get("/published", tutorials.findAllPublished); // Retrieve a single Tutorial with id router.get("/:id", tutorials.findOne); // Update a Tutorial with id router.put("/:id", tutorials.update); // Delete a Tutorial with id router.delete("/:id", tutorials.delete); // Delete all Tutorials router.delete("/", tutorials.deleteAll); app.use('/api/tutorials', router); }; ```