From cb0d7de098f482ba667029f266dd286eab1795b6 Mon Sep 17 00:00:00 2001 From: Matthew Strapp Date: Thu, 29 Apr 2021 12:08:29 -0500 Subject: Add HW6 files --- csci4131/hw7/api/utilities.js | 48 +++++++++++ csci4131/hw7/create_accounts_table.js | 41 ++++++++++ csci4131/hw7/create_contacts_table.js | 44 ++++++++++ csci4131/hw7/dbio.js | 74 +++++++++++++++++ csci4131/hw7/index.js | 109 +++++++++++++++++++++++++ csci4131/hw7/insert_into_accounts_table.js | 45 +++++++++++ csci4131/hw7/login.html | 48 +++++++++++ csci4131/hw7/public/addContact.html | 106 ++++++++++++++++++++++++ csci4131/hw7/public/contacts.html | 78 ++++++++++++++++++ csci4131/hw7/public/stock.html | 124 +++++++++++++++++++++++++++++ csci4131/hw7/public/welcome.html | 53 ++++++++++++ 11 files changed, 770 insertions(+) create mode 100644 csci4131/hw7/api/utilities.js create mode 100644 csci4131/hw7/create_accounts_table.js create mode 100644 csci4131/hw7/create_contacts_table.js create mode 100644 csci4131/hw7/dbio.js create mode 100644 csci4131/hw7/index.js create mode 100644 csci4131/hw7/insert_into_accounts_table.js create mode 100644 csci4131/hw7/login.html create mode 100644 csci4131/hw7/public/addContact.html create mode 100644 csci4131/hw7/public/contacts.html create mode 100644 csci4131/hw7/public/stock.html create mode 100644 csci4131/hw7/public/welcome.html diff --git a/csci4131/hw7/api/utilities.js b/csci4131/hw7/api/utilities.js new file mode 100644 index 0000000..65a087f --- /dev/null +++ b/csci4131/hw7/api/utilities.js @@ -0,0 +1,48 @@ +const express = require('express') +const db = require ('../dbio') +const router = express.Router() +router.use(express.urlencoded({ extended: true })) + +router.get('/contacts', function (req, res) { + db.getContacts().then(function(table) { + res.send(table) + }); +}); + +router.post('/login', async function(req, res) { + var loginInfo = req.body; + var login = loginInfo.login; + var pwd = loginInfo.password; + let rows = []; + + // Query the database tbl_login with login and hashed password + db.query(login, pwd).then(function(rows) { + // Provided there is no error, and the results set is assigned to a variable named rows: + if (rows.length >= 1) {// the length should be 0 or 1, but this will work for now + //success, set the session, return success + req.session.user = login; + res.json({ status: 'success' }); + } else { + res.json({ status: 'fail' }); + } + }); + +}); + +router.get('/logout', function(req, res) { + if(!req.session.user) { + res.send('Session not started, can not logout!'); + } else { + req.session.destroy(); + res.redirect('/login'); + } +}); + +router.post('/addContact', function(req, res) { + var contact = req.body; + db.addContact(contact).then(function() { + res.redirect('/contacts'); + }); +}); + +module.exports = router; diff --git a/csci4131/hw7/create_accounts_table.js b/csci4131/hw7/create_accounts_table.js new file mode 100644 index 0000000..61b2602 --- /dev/null +++ b/csci4131/hw7/create_accounts_table.js @@ -0,0 +1,41 @@ +/* +TO DO: +----- +READ ALL COMMENTS AND REPLACE VALUES ACCORDINGLY +*/ + +const mysql = require("mysql"); + +const dbCon = mysql.createConnection({ + host: "cse-mysql-classes-01.cse.umn.edu", + user: "C4131S21U83", // replace with the database user provided to you + password: "6919", // replace with the database password provided to you + database: "C4131S21U83", // replace with the database user provided to you + port: 3306 +}); + +console.log("Attempting database connection"); +dbCon.connect(function (err) { + if (err) { + throw err; + } + console.log("Connected to database!"); + + const sql = `CREATE TABLE tbl_accounts ( + acc_id INT NOT NULL AUTO_INCREMENT, + acc_name VARCHAR(20), + acc_login VARCHAR(20), + acc_password VARCHAR(200), + PRIMARY KEY (acc_id) + )`; + + console.log("Attempting to create table: tbl_accounts"); + dbCon.query(sql, function (err, result) { + if (err) { + throw err; + } + console.log("Table tbl_accounts created"); + }); + + dbCon.end(); +}); diff --git a/csci4131/hw7/create_contacts_table.js b/csci4131/hw7/create_contacts_table.js new file mode 100644 index 0000000..bcad389 --- /dev/null +++ b/csci4131/hw7/create_contacts_table.js @@ -0,0 +1,44 @@ +/* +TO DO: +----- +READ ALL COMMENTS AND REPLACE VALUES ACCORDINGLY +*/ + +const mysql = require("mysql"); + +const dbCon = mysql.createConnection({ + host: "cse-mysql-classes-01.cse.umn.edu", + user: "C4131S21U83", // replace with the database user provided to you + password: "6919", // replace with the database password provided to you + database: "C4131S21U83", // replace with the database user provided to you + port: 3306 +}); + +console.log("Attempting database connection"); +dbCon.connect(function (err) { + if (err) { + throw err; + } + console.log("Connected to database!"); + + const sql = `CREATE TABLE tbl_contacts ( + contact_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(30), + category VARCHAR(40), + location VARCHAR(300), + contact_info VARCHAR(200), + email VARCHAR(30), + website VARCHAR(300), + website_url VARCHAR(300) + )`; + + console.log("Attempting to create table: tbl_contacts"); + dbCon.query(sql, function (err, result) { + if (err) { + throw err; + } + console.log("Table tbl_accounts created"); + }); + + dbCon.end(); +}); diff --git a/csci4131/hw7/dbio.js b/csci4131/hw7/dbio.js new file mode 100644 index 0000000..302334b --- /dev/null +++ b/csci4131/hw7/dbio.js @@ -0,0 +1,74 @@ +var mysql = require("mysql"); +var bcrypt = require("bcrypt"); + +var connection = mysql.createConnection({ + host: "cse-mysql-classes-01.cse.umn.edu", + user: "C4131S21U83", + password: "6919", + database: "C4131S21U83", + port: 3306 +}); + +connection.connect(function(err) { + if (err) { + throw err; + }; + console.log("Connected to MYSQL database!"); +}); + +function passcheck(user,pass) { + return new Promise(function(resolve, reject) { + connection.query('SELECT * FROM tbl_accounts', function(err, rows, fields) { + let ret = []; + if (err) { + return reject(err); + } + for (var i = 0; i < rows.length; i++) { + if (rows[i].acc_login.localeCompare(user) === 0) { + if (bcrypt.compareSync(pass, rows[i].acc_password)) { + ret += rows[i]; + } + } + } + resolve(ret); + }); + }); +} + +function getContacts() { + return new Promise (function(resolve, reject) { + let conTab = [] + connection.query('SELECT * FROM tbl_contacts', function (err, rows, fields) { + if (err) { + return reject(err); + } + resolve(rows); + }); + }); +} + +function addContacts(contact) { + let newCon = { + name: contact.name, + category: contact.category, + location: contact.location, + contact_info: contact.contact, + email: contact.email, + website_url: contact.website_name, + } + return new Promise(function(resolve, reject) { + connection.query('INSERT tbl_contacts SET ?', newCon, function (err, result) { //Parameterized insert + if (err) throw err; + console.log("Values inserted"); + resolve(); + }); + }); + +} + + + + +exports.addContact = addContacts; +exports.query = passcheck; +exports.getContacts = getContacts; diff --git a/csci4131/hw7/index.js b/csci4131/hw7/index.js new file mode 100644 index 0000000..851b096 --- /dev/null +++ b/csci4131/hw7/index.js @@ -0,0 +1,109 @@ +// YOU CAN USE THIS FILE AS REFERENCE FOR SERVER DEVELOPMENT +const createError = require('http-errors'); + +// Include the express module +const express = require('express'); + +// helps in extracting the body portion of an incoming request stream +var bodyparser = require('body-parser'); + +// Path module - provides utilities for working with file and directory paths. +const path = require('path'); + +// Helps in managing user sessions +const session = require('express-session'); + +// include the mysql module +var mysql = require('mysql'); + +// Bcrypt library for comparing password hashes +const bcrypt = require('bcrypt'); + +// Include the express router. +const utilities = require('./api/utilities'); + +const port = 9001; + +// create an express application +const app = express(); + +// Use express-session +// In-memory session is sufficient for this assignment +app.use(session({ + secret: "csci4131secretkey", + saveUninitialized: true, + resave: false + } +)); + +// middle ware to serve static files +app.use(express.static(path.join(__dirname, 'public'))); + +// server listens on port for incoming connections +app.listen(port, () => console.log('Listening on port', port)); + +app.get('/', function (req, res) { + res.sendFile(path.join(__dirname, 'public/welcome.html')); +}); + +// GET method route for the contacts page. +// It serves contact.html present in public folder +app.get('/contacts', function(req, res) { + if(!req.session.user) { + res.redirect('/login'); + } else { + res.sendFile(path.join(__dirname, 'public/contacts.html')); + } +}); + +app.get('/stocks', function(req, res) { + res.redirect('/stock'); +}) + +app.get('/stock', function (req, res) { + if (!req.session.user) { + res.redirect('/login'); + } else { + res.sendFile(path.join(__dirname, 'public/stock.html')); + } +}); + +app.get('/addContact', function (req, res) { + if (!req.session.user) { + res.redirect('/login'); + } else { + res.sendFile(path.join(__dirname, 'public/addContact.html')); + } +}); + +app.get('/login', function (req, res) { + if (req.session.user) { + res.redirect('/contacts'); + } else { + res.sendFile(path.join(__dirname, 'login.html')); + } +}); + +app.get('/logout', function(req, res) { + res.redirect('/api/logout') +}); + +// Makes Express use a router called utilities +app.use('/api', utilities); + +// function to return the 404 message and error to client +app.use(function (req, res, next) { + next(createError(404)); +}); + +// error handler +app.use(function (err, req, res, next) { + // set locals, only providing error in development + res.locals.message = err.message; + res.locals.error = req.app.get('env') === 'development' ? err : {}; + + // render the error page + res.status(err.status || 500); + // res.render('error'); + res.send(); +}); diff --git a/csci4131/hw7/insert_into_accounts_table.js b/csci4131/hw7/insert_into_accounts_table.js new file mode 100644 index 0000000..3157ce3 --- /dev/null +++ b/csci4131/hw7/insert_into_accounts_table.js @@ -0,0 +1,45 @@ +/* +TO DO: +----- +READ ALL COMMENTS AND REPLACE VALUES ACCORDINGLY +*/ + +const mysql = require("mysql"); +const bcrypt = require('bcrypt'); + +const dbCon = mysql.createConnection({ + host: "cse-mysql-classes-01.cse.umn.edu", + user: "C4131S21U83", // replace with the database user provided to you + password: "6919", // replace with the database password provided to you + database: "C4131S21U83", // replace with the database user provided to you + port: 3306 +}); + +console.log("Attempting database connection"); +dbCon.connect(function (err) { + if (err) { + throw err; + } + + console.log("Connected to database!"); + + const saltRounds = 10; + const myPlaintextPassword = 'tango'; // replace with password chosen by you OR retain the same value + const passwordHash = bcrypt.hashSync(myPlaintextPassword, saltRounds); + + const rowToBeInserted = { + acc_name: 'charlie', // replace with acc_name chosen by you OR retain the same value + acc_login: 'charlie', // replace with acc_login chosen by you OR retain the same value + acc_password: passwordHash + }; + + console.log("Attempting to insert record into tbl_accounts"); + dbCon.query('INSERT tbl_accounts SET ?', rowToBeInserted, function (err, result) { + if (err) { + throw err; + } + console.log("Table record inserted!"); + }); + + dbCon.end(); +}); diff --git a/csci4131/hw7/login.html b/csci4131/hw7/login.html new file mode 100644 index 0000000..9bb80cc --- /dev/null +++ b/csci4131/hw7/login.html @@ -0,0 +1,48 @@ + + + + + + + + + +
+

Login Page

+
+

+
+
+ + +
+
+ + +
+ +
+
+ + + diff --git a/csci4131/hw7/public/addContact.html b/csci4131/hw7/public/addContact.html new file mode 100644 index 0000000..629f9b2 --- /dev/null +++ b/csci4131/hw7/public/addContact.html @@ -0,0 +1,106 @@ + + + + + + + + + + + + + +

+ +
+
+


+
+ +
+
+
+
+
+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Name +
+ +
+
Category +
+ +
+
Location +
+ +
+
Contact Information +
+ +
+
Email +
+ +
+
Website Name +
+ +
+
+ +
+
+
+
+
+
+
+ + diff --git a/csci4131/hw7/public/contacts.html b/csci4131/hw7/public/contacts.html new file mode 100644 index 0000000..5cd2907 --- /dev/null +++ b/csci4131/hw7/public/contacts.html @@ -0,0 +1,78 @@ + + + + + + + + + + + +

+ +
+ + + + + + + + + + + + +
NameCategoryLocationContact InformationEmailWebsite
(URL)
+
+ + + diff --git a/csci4131/hw7/public/stock.html b/csci4131/hw7/public/stock.html new file mode 100644 index 0000000..d63b233 --- /dev/null +++ b/csci4131/hw7/public/stock.html @@ -0,0 +1,124 @@ + + + + + + + + + + + + + +

+ +
+
+
Welcome to Stock Page
+
+
+ +
+
+
+ + + + + + + + + + +
Company +
+ +
+
+ +
+
+
+
+ +
+
+
+ +
+ + + + + + + +
+      
+ +
Company-MetaDataStock-Info
+
+ + + + diff --git a/csci4131/hw7/public/welcome.html b/csci4131/hw7/public/welcome.html new file mode 100644 index 0000000..095023e --- /dev/null +++ b/csci4131/hw7/public/welcome.html @@ -0,0 +1,53 @@ + + + + + + + + + Welcome to Node.js + + + + +
+

Welcome to Express (Node.js)

+

The objective of this assignment is to develop a basic website with:

+

Express which is a Node.js web application framework.

+
+

Following are some useful resources:

+ + + +
+
+ +
+ +
+
+
+ +
+
+
+ + -- cgit v1.2.3