Skip to content

NW6 | Fikret Ellek | Module-Database | E-Commerce-Api | Week 4 #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions E-Commerce-API/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=cyf_ecommerce
DB_USERNAME=admin
DB_PASSWORD=
8 changes: 0 additions & 8 deletions E-Commerce-API/.env.example

This file was deleted.

10 changes: 10 additions & 0 deletions E-Commerce-API/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
const express = require("express");
const { connectDB, disconnectDb } = require("./db.js");
const router = require("./router.js");
const bodyParser = require("body-parser");

const app = express();
// connectDB();
app.use(bodyParser.json());
app.use("/", router);
// app.listen(3100, () => {
// console.log("app is listening on port 3100");
// });
// Your code to run the server should go here
// Don't hardcode your DB password in the code or upload it to GitHub! Never ever do this ever.
// Use environment variables instead:
Expand Down
43 changes: 43 additions & 0 deletions E-Commerce-API/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const pg = require("pg");

const { Pool } = pg;

const pool = new Pool({
connectionString: "postgresql://admin:3214@localhost:5432/cyf_ecommerce",

Check failure

Code scanning / SonarCloud

PostgreSQL database passwords should not be disclosed High

Make sure this PostgreSQL database password gets changed and removed from the code. See more on SonarCloud
connectionTimeoutMillis: 5000,
ssl: false,
});

const connectDB = async () => {
if (!pool) {
return;
}

let client;
try {
client = await pool.connect();
} catch (err) {
console.log(err);
throw err;
}
console.log(`postgres connected to ${client.database} on port 5432`);
client.release();
};

const disconnectDb = () => {
if (!pool) {
return;
}
pool.end();
};

module.exports = {
connectDB,
disconnectDb,
query: (...args) => {
if (!pool) {
return;
}
return pool.query.apply(pool, args);
},
};
Loading