This sample was created using the following techologies and they must be installed before proceeding.
Configure the MariaDB connection by adding an .env file to the Node.js project.
Example implementation:
DB_HOST=<host_address>
DB_PORT=<port_number>
DB_USER=<username>
DB_PASS=<password>
DB_NAME=<database>
Configuring db.js
The environmental variables from .env
are used within the db.js for the MariaDB Node.js Connector configuration pool settings:
var mariadb = require('mariadb');
require('dotenv').config();
const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
multipleStatements: true,
connectionLimit: 5
});
Configuring db.js for the MariaDB cloud database service SkySQL
MariaDB SkySQL requires SSL additions to connection. It's as easy as 1-2-3 (steps below).
var mariadb = require('mariadb');
require('dotenv').config();
// 1.) Access the Node File System package
const fs = require("fs");
// 2.) Retrieve the Certificate Authority chain file (wherever you placed it - notice it's just in the Node project root here)
const serverCert = [fs.readFileSync("skysql_chain_t.pem", "utf8")];
var pools = [
mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: process.env.DB_PORT,
database: process.env.DB_NAME,
multipleStatements: true,
connectionLimit: 5,
// 3.) Add an "ssl" property to the connection pool configuration, using the serverCert const defined above
ssl: {
ca: serverCert
}
})
];
Once you have retrieved a copy of the code you're ready to build and run the project! However, before running the code it's important to point out that the application uses several Node Packages.
Executing the CLI command
$ npm install
Doing this targets relative package.json
file and install all dependencies.
IMPORTANT: Be sure that the Node modules are installed for the client. This can be done manually executing the following CLI command for client:
$ npm install
Once you've pulled down the code and have verified that all of the required Node packages are installed you're ready to run the application!
- Execute the following CLI command
$ npm start
The following steps also exist within the "Build and run" section of the root README, and are for startin the React.js project once this API project has been started.
- Navigate to the ../../client folder and execute the following CLI command to start the React.js application.
$ npm start
- Open a browser window and navigate to http://localhost:3000.