diff options
author | RossTheRoss <mstrapp@protonmail.com> | 2021-04-13 14:22:16 -0500 |
---|---|---|
committer | RossTheRoss <mstrapp@protonmail.com> | 2021-04-13 14:22:16 -0500 |
commit | 20dba29f9c01e6ff4142fc8dd73d7e7c33a42034 (patch) | |
tree | 2081f57a595a438a40904210139c495defd994ff | |
parent | oops (diff) | |
download | homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.tar homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.tar.gz homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.tar.bz2 homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.tar.lz homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.tar.xz homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.tar.zst homework-20dba29f9c01e6ff4142fc8dd73d7e7c33a42034.zip |
start hw6
Diffstat (limited to '')
-rw-r--r-- | csci4131/hw6/strap012_hw6/README.txt | 2 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/api/utilities.js | 10 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/create_accounts_table.js | 41 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/create_contacts_table.js | 44 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/index.js | 75 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/insert_into_accounts_table.js | 45 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/package.json | 14 | ||||
-rw-r--r-- | csci4131/hw6/strap012_hw6/public/welcome.html | 54 |
8 files changed, 285 insertions, 0 deletions
diff --git a/csci4131/hw6/strap012_hw6/README.txt b/csci4131/hw6/strap012_hw6/README.txt new file mode 100644 index 0000000..e056441 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/README.txt @@ -0,0 +1,2 @@ +username: charlie +password: tango
\ No newline at end of file diff --git a/csci4131/hw6/strap012_hw6/api/utilities.js b/csci4131/hw6/strap012_hw6/api/utilities.js new file mode 100644 index 0000000..6b6cec6 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/api/utilities.js @@ -0,0 +1,10 @@ +const express = require('express')
+const router = express.Router()
+
+router.get('/contacts', function (req, res) {
+ // TODO: Implement code to fetch contacts from the database
+});
+
+// TODO: Add implementation for other necessary end-points
+
+module.exports = router;
\ No newline at end of file diff --git a/csci4131/hw6/strap012_hw6/create_accounts_table.js b/csci4131/hw6/strap012_hw6/create_accounts_table.js new file mode 100644 index 0000000..61b2602 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/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/hw6/strap012_hw6/create_contacts_table.js b/csci4131/hw6/strap012_hw6/create_contacts_table.js new file mode 100644 index 0000000..bcad389 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/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/hw6/strap012_hw6/index.js b/csci4131/hw6/strap012_hw6/index.js new file mode 100644 index 0000000..a81fbd1 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/index.js @@ -0,0 +1,75 @@ +// 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 9002 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) { + // TODO: Add Implementation +}); + +// TODO: Add implementation for other necessary end-points + +// 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(); +});
\ No newline at end of file diff --git a/csci4131/hw6/strap012_hw6/insert_into_accounts_table.js b/csci4131/hw6/strap012_hw6/insert_into_accounts_table.js new file mode 100644 index 0000000..3157ce3 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/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/hw6/strap012_hw6/package.json b/csci4131/hw6/strap012_hw6/package.json new file mode 100644 index 0000000..fb99a59 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/package.json @@ -0,0 +1,14 @@ +{ + "name": "strap012_hw6", + "version": "1.0.0", + "description": "Assignment 6", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "strap012", + "license": "ISC", + "dependencies": { + "express": "^4.17.1" + } +} diff --git a/csci4131/hw6/strap012_hw6/public/welcome.html b/csci4131/hw6/strap012_hw6/public/welcome.html new file mode 100644 index 0000000..50ae883 --- /dev/null +++ b/csci4131/hw6/strap012_hw6/public/welcome.html @@ -0,0 +1,54 @@ +<!doctype html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> + <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> + <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> + <link rel="stylesheet" type="text/css" href="/css/style.css"> + <title>Welcome to Node.js</title> + <style type="text/css"> + .jumbotron { + text-align: center; + } + </style> +</head> + +<body> +<div class="jumbotron" style="background: DarkSeaGreen !important"> + <h1>Welcome to Express (Node.js)</h1> + <p>The objective of this assignment is to develop a basic website with:</p> + <p><b>Express</b> which is a Node.js web application framework.</p> + <br/> + <p>Following are some useful resources:</p> + + <ul style="list-style-type:none"> + <li><a href="https://expressjs.com/">Express website</a></li> + <li><a href="https://expressjs.com/en/starter/installing.html">Express installation</a></li> + <li><a href="https://expressjs.com/en/starter/hello-world.html">Hello world example</a></li> + <li><a href="https://expressjs.com/en/starter/basic-routing.html">Basic routing</a></li> + <li><a href="https://expressjs.com/en/starter/static-files.html">Serving static files in Express</a></li> + <li><a href="https://expressjs.com/en/starter/faq.html">FAQ</a></li> + <li><a href="https://expressjs.com/en/guide/routing.html">Routing</a></li> + <li><a href="https://expressjs.com/en/4x/api.html">API Reference</a></li> + <li><a href="https://expressjs.com/en/resources/books-blogs.html">Books and blogs</a></li> + <li><a href="https://docs.npmjs.com/getting-started/what-is-npm">NPM introduction</a></li> + </ul> + + <br/> + <br/> + +</div> + +<div class="row"> + <div class="col-md-1"></div> + <div class="col-md-10"> + <button id="myButton" type="button" class="btn btn-primary btn-block" onclick="location.href ='/login'"> + Navigate to website + </button> + </div> + <div class="col-md-1"></div> +</div> +</body> +</html> |