3.2 KiB
Orders - Node.js API
Environment and compatibility
This sample was created using the following techologies:
Getting started with the app
Configure the code
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,
multipleStatements: true,
connectionLimit: 5
});
Configuring db.js for MariaDB SkySQL
MariaDB SkySQL uses 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_1,
user: process.env.DB_USER_1,
password: process.env.DB_PASS_1,
port: process.env.DB_PORT_1,
database: process.env.DB_NAME_1,
multipleStatements: true,
connectionLimit: 5,
// 3.) Add an "ssl" property to the connection pool configuration, using the serverCert const defined above
ssl: {
ca: serverCert
}
})
];
Build the code
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
Run the app
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
- Open a browser window and navigate to http://localhost:3000.