카테고리 없음

Heroku mysql + Mysql workstation +node js

EasyCoding 2021. 2. 19. 14:58
728x90

Heroku  Mysql

 

 

mysql://bf307ee4791b65:f9ebacd1@us-cdbr-east-03.cleardb.com/heroku_796fc805f680d4a?reconnect=true

username: bf307ee4791b65
password: f9ebacd1
host: us-cdbr-east-03.cleardb.com
database: heroku_796fc805f680d4a

mkdir hjsql_test

code hjsql_test

npm init -y

open package.json

{
  "
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
}

npm i --save prohairesis :MySQL database class to abstract pooling and prepared statements

.env

CLEARDB_DATABASE_URL=mysql://bf307ee4791b65:f9ebacd1@us-cdbr-east-03.cleardb.com/heroku_796fc805f680d4a?reconnect=true

npm i --save dotenv

env.js

require('dotenv').config();

module.exports = {
    CLEARDB_DATABASE_URL: process.env.CLEARDB_DATABASE_URL
};

app.js

const { Prohairesis } = require('prohairesis');
const env = require('./env');
//console.log(env);;
const database = new Prohairesis(env.CLEARDB_DATABASE_URL);

database
    .query(`
    SELECT * FROM t_seller
    `).then((res) => {
        console.log(res);
    })
    .catch((e) => {
        console.error(e);
    })
    .finally(
        () => {
            database.close();
        }
    );

npm i --save body-parser morgan

morgan: HTTP request logger middleware for node.js