REST API with Express.js
Database API Example
Käytetään esimerkkiä staticApiExample pohjana ja muokataan sovellusta, niin että data haetaan tietokannasta.
  1. Luo kansio databaseApiExample
  2. Kopioi staticApiExamplen tiedostot em. kansioon
  3. Asenna MySQL moduuli komennolla
    npm install mysql2
    
  4. Luo tiedosto database.js, jossa konfiguroidaan yhteys tietokantaan
  5. Muokataan book_model.js tiedostoa, niin että data haetaan tietokannasta ja tietokantaan voidaan lisätä, muokata ja poistaa dataa

Tietokanta

Käytetään tietokantaa library ja luodaan käyttäjä tunnus seuraavalla koodilla
CREATE USER 'netuser'@'localhost' IDENTIFIED BY 'netpass';
GRANT ALL ON library.* TO 'netuser'@'localhost';

We will use the staticApiExample as a base and modify the application so that data is fetched from a database instead of a static array.
  1. Create a folder named databaseApiExample.
  2. Copy the files from **staticApiExample** into this new folder.
  3. Install the MySQL module by running the command:
    npm install mysql2
  4. Create a new file named **database.js** to configure the database connection.
  5. Modify **book_model.js** so that data is retrieved from the database, and allow adding, updating, and deleting data.

Database

We will use a database named library and create a user account with the following SQL commands:
 
CREATE USER 'netuser'@'localhost' IDENTIFIED BY 'netpass'; 
GRANT ALL ON library.* TO 'netuser'@'localhost'; 



Toggle Menu