External Data Sources

 

Node.JS can access external data-sources but it should be used wisely.  A Node.JS application runs in the context of the front end.  Frontend components should NOT be directly accessing data-sources.  However, they can access some kind of local-storage system for the handling of temporary data, but even then it should be done with care and a lot of thought in terms of the long term scalability.

Using the File System

Use the module "fs", visit https://nodejs.org/docs/latest-v13.x/api/fs.html

const fs = require("fs");


Using a database

Use the module that matches the database you want to access, visit https://expressjs.com/en/guide/database-integration.html

var mysql = require('mysql') var connection = mysql.createConnection({ host: 'localhost', user: 'root', password: 'ppp', database: 'db_grad_cs_1917' }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); });

We are running MySQL from a Docker container, the container can be started using the following command

docker run -p 3307:3306 --name mysql-populated -e MYSQL_ROOT_PASSWORD=ppp -d mysql-db-populated:latest

parameter

meaning

parameter

meaning

-p 3307:3306

bind the container port 3306 to host port 3307

--name mysql-populated

the runtime name of the container, can be used instead of image id

-e

an environment variable to be passed into the container

mysql-db-populated:latest

the actual image name, can be found using docker images

When you run the above script it fails with an error indicating that password authorisation is not supported. You need to change the MySQL plugin from auth_socket to mysql_native_password.

Follow these steps

For Docker

Check the IP address of the container that is hosting MySQL. Add this IP address to the MySQL users table for root user

Be sure to give the user for the new IP the correct rights