initial commit, database config working, pug template basic config, generated site working

This commit is contained in:
Sean Clarke 2020-01-04 15:44:11 -05:00
parent b4121ea146
commit 191e7e825a
16 changed files with 9209 additions and 0 deletions

41
app.js Normal file
View File

@ -0,0 +1,41 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
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');
});
module.exports = app;

90
bin/www Executable file
View File

@ -0,0 +1,90 @@
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('unesco-tracker:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

7165
data/unesco.xml Normal file

File diff suppressed because one or more lines are too long

45
database/build.js Normal file
View File

@ -0,0 +1,45 @@
var con = require('./db');
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("CREATE DATABASE unesco", function (err, res) {
if (err) throw err;
console.log('"unesco" Database created.');
});
con.query("USE unesco", function (err, res) {
if (err) throw err;
console.log('"unesco" Database selected.');
});
con.query("CREATE TABLE sites(\
id int NOT NULL AUTO_INCREMENT,\
category varchar(255),\
in_danger bool,\
date_inscribed int,\
unesco_url varchar(255),\
latitude varchar(255),\
longitude varchar(255),\
description varchar(5000),\
site varchar(255),\
unesco_unique int,\
PRIMARY KEY (id)\
)", function(err, res){
if (err) throw err;
console.log("sites Table Created.");
});
con.query("CREATE TABLE visits(\
id int NOT NULL AUTO_INCREMENT,\
date varchar(255),\
img varchar(255),\
site_id int,\
FOREIGN KEY (site_id) REFERENCES sites(id),\
PRIMARY KEY (id)\
)", function(err, res){
if (err) throw err;
console.log("visits Table Created.");
process.exit();
});
});

11
database/db.js Normal file
View File

@ -0,0 +1,11 @@
var mysql = require('mysql');
// This information is only used for testing - change to production before deploying
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "root"
// database: "unesco"
});
module.exports = con;

11
database/drop-database.js Normal file
View File

@ -0,0 +1,11 @@
var con = require('./db');
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("DROP DATABASE unesco", function (err, result) {
if (err) throw err;
console.log('"unesco" Database dropped');
process.exit();
});
});

35
database/fill-database.js Normal file
View File

@ -0,0 +1,35 @@
var con = require('./db');
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("USE unesco", function (err, res) {
if (err) throw err;
console.log('"unesco" Database selected.');
});
sql = "INSERT INTO sites (category, date_inscribed, unesco_url, latitude, longitude, description, site, unesco_unique) VALUES ?";
val = [
['Natural','2007','https://whc.unesco.org/en/list/1133','49.0097222222', '22.3388888889', '<p><span>This transboundary property stretches over 12 countries. Since the end of the last Ice Age, European Beech spread from a few isolated refuge areas in the Alps, Carpathians</span><span>, Dinarides</span><span>, Mediterranean and Pyrenees over a short period of a few thousand years in a process that is still ongoing. The successful expansion across a whole continent is related to the trees </span><span>adaptability and tolerance of different climatic, geographical and physical conditions. </span></p>', 'Ancient and Primeval Beech Forests of the Carpathians and Other Regions of Europe', '2152'],
['Cultural','2014','https://whc.unesco.org/en/list/1459','-18.2500000000', '-69.5916666667', '<p>This site is an extensive Inca communication, trade and defence network of roads covering 30,000 km. Constructed by the Incas over several centuries and partly based on pre-Inca infrastructure, this extraordinary network through one of the worlds most extreme geographical terrains linked the snow-capped peaks of the Andes at an altitude of more than 6,000 m to the coast, running through hot rainforests, fertile valleys and absolute deserts. It reached its maximum expansion in the 15th century, when it spread across the length a significance', 'Qhapaq Ñan, Andean Road System', '2003']
];
con.query(sql, [val], function(err, res){
if (err) throw err;
console.log("sites" + ": records inserted: " + res.affectedRows);
});
sql = "INSERT INTO visits (date, img, site_id) VALUES ?";
val = [
['10-15-2019','https://seanland.ca', '1'],
['10-17-2019','https://seanland.ca', '1']
];
con.query(sql, [val], function(err, res){
if (err) throw err;
console.log("visits" + ": records inserted: " + res.affectedRows);
process.exit();
});
});

1737
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "unesco-tracker",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon ./bin/www",
"build-db": "node database/build.js",
"drop-db": "node database/drop-database.js",
"fill-db": "node database/fill-database.js"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"mysql": "^2.17.1",
"nodemon": "^2.0.2",
"pug": "^2.0.4",
"xml2js": "^0.4.23"
}
}

View File

@ -0,0 +1,8 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}

9
routes/index.js Normal file
View File

@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index', { title: 'UNESCO Tracker - Sean Clarke' });
});
module.exports = router;

9
routes/users.js Normal file
View File

@ -0,0 +1,9 @@
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with a resource');
});
module.exports = router;

6
views/error.pug Normal file
View File

@ -0,0 +1,6 @@
extends layout
block content
h1= message
h2= error.status
pre #{error.stack}

1
views/footer.pug Normal file
View File

@ -0,0 +1 @@
h4= "Sean Clarke"

7
views/index.pug Normal file
View File

@ -0,0 +1,7 @@
extends layout
block content
h1= title
p Welcome to #{title}
include footer.pug

11
views/layout.pug Normal file
View File

@ -0,0 +1,11 @@
doctype html
html
head
meta(charset='utf-8')
title= title
meta(name='description', content='UNESCO World Heritage Site Visits - Sean Clarke')
meta(name='author', content='Sean Clarke')
link(rel='shortcut icon' href='favicon.ico' type='image/x-icon')
link(rel='stylesheet', href='/stylesheets/main.css')
body
block content