first commit
This commit is contained in:
commit
58f45f8947
25
.gitignore
vendored
Normal file
25
.gitignore
vendored
Normal file
|
@ -0,0 +1,25 @@
|
|||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
pnpm-debug.log*
|
||||
lerna-debug.log*
|
||||
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
.env
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
!.vscode/extensions.json
|
||||
.idea
|
||||
.DS_Store
|
||||
*.suo
|
||||
*.ntvs*
|
||||
*.njsproj
|
||||
*.sln
|
||||
*.sw?
|
85
README.md
Normal file
85
README.md
Normal file
|
@ -0,0 +1,85 @@
|
|||
# use knex.js as ES6 and work with .env
|
||||
|
||||
# The below instructions are intended for command line execution while working with node server
|
||||
|
||||
install dotenv and knex.js
|
||||
|
||||
npm install knex --save
|
||||
|
||||
In the project root directory, create a folder called 'db' and navigate to this 'db' folder:
|
||||
|
||||
execute the following command:
|
||||
|
||||
npx knex init
|
||||
|
||||
A 'knexfile.js' will be created in this directory.
|
||||
|
||||
Change the file extension to .cjs
|
||||
|
||||
you will need to add the below line in your package.json file:
|
||||
|
||||
"type": "module",
|
||||
|
||||
create 2 subfolders inside 'db' folder, called "migrations" and "seeds"
|
||||
|
||||
# data in .env file
|
||||
|
||||
This file should be placed in the project root directory
|
||||
|
||||
DEV_USER=
|
||||
DEV_DB=
|
||||
DEV_PASSWORD=
|
||||
DEV_HOST=localhost
|
||||
DEV_PORT=5432
|
||||
|
||||
# knexfile.cjs configuration
|
||||
|
||||
// check current working directory
|
||||
const dir = process.cwd();
|
||||
// check current running environment
|
||||
const env = process.env.ENVIRONMENT || "";
|
||||
|
||||
// current settings do not work for production mode
|
||||
// TODO production mode checking
|
||||
const config =
|
||||
env === "development"
|
||||
? require("dotenv").config({ path: dir + "/.env" })
|
||||
: require("dotenv").config({ path: "../.env" });
|
||||
|
||||
// the database details here //
|
||||
exports.development = {}
|
||||
exports.production = {}
|
||||
|
||||
# below instructions are used on a terminal window navigate to 'db' folder
|
||||
|
||||
create a migration file type:
|
||||
|
||||
knex migrate:make create_XYZ_table --migrations-directory migrations
|
||||
|
||||
make sure to rename the file extensions to .cjs
|
||||
|
||||
execute migration type:
|
||||
|
||||
knex migrate:latest --knexfile knexfile.cjs --migrations-directory migrations
|
||||
|
||||
execute rollback type:
|
||||
|
||||
knex migrate:rollback --knexfile knexfile.cjs --migrations-directory migrations
|
||||
|
||||
knex URL: https://knexjs.org/guide/migrations.html#migration-cli
|
||||
|
||||
create a seed file type:
|
||||
|
||||
knex seed:make 01_XYZ_test_data --knexfile knexfile.cjs
|
||||
|
||||
make sure to rename the file extensions to .cjs
|
||||
|
||||
execute the seed file type:
|
||||
|
||||
knex seed:run --knexfile knexfile.cjs
|
||||
|
||||
# to run the node server
|
||||
|
||||
navigate back to the project root directory and type:
|
||||
|
||||
ENVIRONMENT=development npm run dev
|
33
app.js
Normal file
33
app.js
Normal file
|
@ -0,0 +1,33 @@
|
|||
import express from "express";
|
||||
import SubscriberRouter from "./routes/subscribers.js";
|
||||
import cors from "cors";
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 5005;
|
||||
|
||||
// middleware
|
||||
app.use(express.json());
|
||||
app.use(cors());
|
||||
|
||||
//routes
|
||||
app.use("/api/subscriber", SubscriberRouter);
|
||||
|
||||
app.use(express.static("public"));
|
||||
|
||||
/* landing page GET request */
|
||||
app.get("/", (req, res) => {
|
||||
/// send the static file
|
||||
res.sendFile("index.html", (err) => {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.use((req, res) => {
|
||||
res.status(400).send("Bad Request. Route not found");
|
||||
});
|
||||
|
||||
app.listen(PORT, () =>
|
||||
console.log(`Express app is listening on port ${PORT}!`)
|
||||
);
|
54
controllers/subscribers.js
Normal file
54
controllers/subscribers.js
Normal file
|
@ -0,0 +1,54 @@
|
|||
import {
|
||||
getAllSubscribers,
|
||||
addOneSubscriber,
|
||||
selectOneSubscriber,
|
||||
deleteOneSubscriber
|
||||
} from "../models/subscribers.js";
|
||||
|
||||
async function getSubscribers(req, res) {
|
||||
try {
|
||||
const results = await getAllSubscribers();
|
||||
return res.status(200).json(results);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function addSubscriber(req, res) {
|
||||
console.log(req.body);
|
||||
try {
|
||||
const results = await addOneSubscriber(req.body);
|
||||
|
||||
return res.status(200).json(results);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function getSubscriber(req, res) {
|
||||
console.log(req.params);
|
||||
try {
|
||||
const results = await selectOneSubscriber(req.params);
|
||||
|
||||
return res.status(200).json(results);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSubscriber(req, res) {
|
||||
console.log(req.body);
|
||||
try {
|
||||
const results = await deleteOneSubscriber(req.body);
|
||||
|
||||
return res.status(200).json(results);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return res.status(500).json(error);
|
||||
}
|
||||
}
|
||||
|
||||
export { getSubscribers, addSubscriber, getSubscriber, deleteSubscriber };
|
11
db/db.js
Normal file
11
db/db.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import knex from "knex";
|
||||
import { development, production } from "./knexfile.cjs";
|
||||
|
||||
const env = process.env.ENVIRONMENT || "development";
|
||||
|
||||
console.log(env);
|
||||
console.log(development);
|
||||
|
||||
const db = env === "development" ? knex(development) : knex(production);
|
||||
|
||||
export default db;
|
98
db/knexfile.cjs
Normal file
98
db/knexfile.cjs
Normal file
|
@ -0,0 +1,98 @@
|
|||
// Update with your config settings.
|
||||
|
||||
const dir = process.cwd();
|
||||
|
||||
const env = process.env.ENVIRONMENT || "";
|
||||
|
||||
const config =
|
||||
env === "development"
|
||||
? require("dotenv").config({ path: dir + "/.env" })
|
||||
: require("dotenv").config({ path: "../.env" });
|
||||
|
||||
//const config = require("dotenv").config({ path: dir + "/.env" });
|
||||
|
||||
console.log(config);
|
||||
|
||||
/**
|
||||
* @type { Object.<string, import("knex").Knex.Config> }
|
||||
*/
|
||||
|
||||
exports.development = {
|
||||
client: "postgresql",
|
||||
connection: {
|
||||
database: config.parsed.DEV_DB, // env var: PGDATABASE YOUR UEA username
|
||||
user: config.parsed.DEV_USER, // env var: PGUSER YOUR UEA username
|
||||
password: config.parsed.DEV_PASSWORD // env var: PGPASSWORD YOUR UEA password
|
||||
},
|
||||
pool: {
|
||||
min: 2,
|
||||
max: 10
|
||||
},
|
||||
migrations: {
|
||||
tableName: "knex_migrations"
|
||||
},
|
||||
seeds: {
|
||||
directory: "./seeds"
|
||||
}
|
||||
};
|
||||
|
||||
exports.production = {
|
||||
client: "postgresql",
|
||||
connection: {
|
||||
host: process.env.DEPLOYBOT_DB_HOST,
|
||||
database: process.env.DEPLOYBOT_DB_NAME,
|
||||
user: process.env.DEPLOYBOT_DB_USER,
|
||||
password: process.env.DEPLOYBOT_DB_PASS,
|
||||
port: process.env.DEPLOYBOT_DB_PORT
|
||||
},
|
||||
pool: {
|
||||
min: 2,
|
||||
max: 10
|
||||
},
|
||||
migrations: {
|
||||
tableName: "knex_migrations"
|
||||
}
|
||||
};
|
||||
|
||||
// module.exports = {
|
||||
// development: {
|
||||
// client: "postgresql",
|
||||
// connection: {
|
||||
// database: "6057", // env var: PGDATABASE YOUR UEA username
|
||||
// user: "postgres", // env var: PGUSER YOUR UEA username
|
||||
// password: "12345" // env var: PGPASSWORD YOUR UEA password
|
||||
// }
|
||||
// },
|
||||
|
||||
// staging: {
|
||||
// client: "postgresql",
|
||||
// connection: {
|
||||
// database: "my_db",
|
||||
// user: "username",
|
||||
// password: "password"
|
||||
// },
|
||||
// pool: {
|
||||
// min: 2,
|
||||
// max: 10
|
||||
// },
|
||||
// migrations: {
|
||||
// tableName: "knex_migrations"
|
||||
// }
|
||||
// },
|
||||
|
||||
// production: {
|
||||
// client: "postgresql",
|
||||
// connection: {
|
||||
// database: "my_db",
|
||||
// user: "username",
|
||||
// password: "password"
|
||||
// },
|
||||
// pool: {
|
||||
// min: 2,
|
||||
// max: 10
|
||||
// },
|
||||
// migrations: {
|
||||
// tableName: "knex_migrations"
|
||||
// }
|
||||
// }
|
||||
// };
|
21
db/migrations/20241021122628_create_subscribers_table.cjs
Normal file
21
db/migrations/20241021122628_create_subscribers_table.cjs
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.createTable("subscribers", (table) => {
|
||||
table.increments("id");
|
||||
table.string("firstname", 50).notNullable().defaultTo("");
|
||||
table.string("lastname", 50).notNullable().defaultTo("");
|
||||
table.string("email", 100).notNullable().defaultTo("");
|
||||
table.timestamps(true, true);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.dropTable("subscribers");
|
||||
};
|
21
db/migrations/20241021123023_create_users_table.cjs
Normal file
21
db/migrations/20241021123023_create_users_table.cjs
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.createTable("users", (table) => {
|
||||
table.increments("id");
|
||||
table.string("firstname", 50).notNullable().defaultTo("");
|
||||
table.string("lastname", 50).notNullable().defaultTo("");
|
||||
table.string("email", 100).notNullable().defaultTo("");
|
||||
table.timestamps(true, true);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.dropTable("users");
|
||||
};
|
15
db/migrations/20241021130056_create_logins_table.cjs
Normal file
15
db/migrations/20241021130056_create_logins_table.cjs
Normal file
|
@ -0,0 +1,15 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.up = function(knex) {
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.down = function(knex) {
|
||||
|
||||
};
|
13
db/seeds/01_subscribers_test_data.cjs
Normal file
13
db/seeds/01_subscribers_test_data.cjs
Normal file
|
@ -0,0 +1,13 @@
|
|||
/**
|
||||
* @param { import("knex").Knex } knex
|
||||
* @returns { Promise<void> }
|
||||
*/
|
||||
exports.seed = async function (knex) {
|
||||
// Deletes ALL existing entries
|
||||
// Deletes ALL existing entries
|
||||
await knex("subscribers").del();
|
||||
await knex("subscribers").insert([
|
||||
{ firstname: "Jeannette", lastname: "Chin", email: "jc@jc.com" },
|
||||
{ firstname: "Jo", lastname: "Blog", email: "jo@blog.com" }
|
||||
]);
|
||||
};
|
51
models/subscribers.js
Normal file
51
models/subscribers.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
import db from "../db/db.js";
|
||||
|
||||
const getAllSubscribers = async () => {
|
||||
const results = await db
|
||||
.select("*")
|
||||
.from("subscribers")
|
||||
.orderBy([{ column: "lastname", order: "asc" }]);
|
||||
console.log(results);
|
||||
return results;
|
||||
};
|
||||
|
||||
const addOneSubscriber = async (data) => {
|
||||
console.log(data);
|
||||
const { firstname, lastname, email } = data;
|
||||
|
||||
const [id] = await db("subscribers")
|
||||
.insert({
|
||||
firstname: firstname,
|
||||
lastname: lastname,
|
||||
email: email
|
||||
})
|
||||
.returning("id");
|
||||
|
||||
return id;
|
||||
};
|
||||
|
||||
const selectOneSubscriber = async (data) => {
|
||||
console.log(data);
|
||||
const { id } = data;
|
||||
|
||||
const result = await db.select("*").from("subscribers").where("id", id);
|
||||
console.log(`..resuts .. ${Object.keys(result)}`);
|
||||
return result;
|
||||
};
|
||||
const deleteOneSubscriber = async (data) => {
|
||||
console.log(data);
|
||||
const { id } = data;
|
||||
|
||||
const [result] = await db("subscribers")
|
||||
.where("id", id)
|
||||
.del()
|
||||
.returning(["id", "firstname", "lastname", "email"]);
|
||||
return result;
|
||||
};
|
||||
|
||||
export {
|
||||
getAllSubscribers,
|
||||
addOneSubscriber,
|
||||
selectOneSubscriber,
|
||||
deleteOneSubscriber
|
||||
};
|
1466
package-lock.json
generated
Normal file
1466
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
23
package.json
Normal file
23
package.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "y",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "app.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "node app.js",
|
||||
"dev": "nodemon app.js"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.1.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.21.1",
|
||||
"knex": "^3.1.0",
|
||||
"pg": "^8.13.0"
|
||||
}
|
||||
}
|
17
public/index.html
Normal file
17
public/index.html
Normal file
|
@ -0,0 +1,17 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Adv Web Development backend server</title>
|
||||
|
||||
<script
|
||||
src="https://kit.fontawesome.com/67bd86ffdc.js"
|
||||
crossorigin="anonymous"
|
||||
></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Advanced Wev Development</h1>
|
||||
<h2>Frontend and Backend intergration</h2>
|
||||
<p>by Jeannette Chin</p>
|
||||
</body>
|
||||
</html>
|
31
routes/subscribers.js
Normal file
31
routes/subscribers.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
import express from "express";
|
||||
import {
|
||||
getSubscribers,
|
||||
addSubscriber,
|
||||
getSubscriber,
|
||||
deleteSubscriber
|
||||
} from "../controllers/subscribers.js";
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
/* GET a single subscriber request */
|
||||
router.get("/:id", (req, res) => {
|
||||
getSubscriber(req, res);
|
||||
});
|
||||
|
||||
/* default path GET request */
|
||||
router.get("/", (req, res) => {
|
||||
getSubscribers(req, res);
|
||||
});
|
||||
|
||||
/* add a subscriber */
|
||||
router.post("/add", (req, res) => {
|
||||
addSubscriber(req, res);
|
||||
});
|
||||
|
||||
/* delete a subscriber */
|
||||
router.delete("/", (req, res) => {
|
||||
deleteSubscriber(req, res);
|
||||
});
|
||||
|
||||
export default router;
|
Loading…
Reference in New Issue
Block a user