diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..83b6c46 --- /dev/null +++ b/README.txt @@ -0,0 +1,5 @@ +The index.html file comtains the front-end form in HTML5 with the +validation of the fields done in javascript and Node.js is used on +the Server side for connection to the MySQL database and to populate the database fields. +The server side code is present in server.js. The script for instantiating the XMLHttpRequest +object is present in script.js \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..b4b5055 --- /dev/null +++ b/index.html @@ -0,0 +1,45 @@ + + + + + Simple Twitter + + + +
+

Simple Twitter

+
+
+ + +
+
+ + +
+ +
+
+
+ + diff --git a/script.js b/script.js new file mode 100644 index 0000000..54b3fe2 --- /dev/null +++ b/script.js @@ -0,0 +1,50 @@ +/** + * Created by Aloma on 10/18/2016. + */ +function getXMLHTTPRequest() +{ + var request; + + // Lets try using ActiveX to instantiate the XMLHttpRequest object + try{ + request = new ActiveXObject("Microsoft.XMLHTTP"); + }catch(ex1){ + try{ + request = new ActiveXObject("Msxml2.XMLHTTP"); + }catch(ex2){ + request = null; + } + } + // If the previous didn't work, lets check if the browser natively support XMLHttpRequest + if(!request && typeof XMLHttpRequest != "undefined"){ + //The browser does, so lets instantiate the object + request = new XMLHttpRequest(); + } + return request; +} +function loadURL(filename, callback) +{ + var aXMLHttpRequest = getXMLHTTPRequest(); + var allData; + if (aXMLHttpRequest) + { + aXMLHttpRequest.open("GET", filename, true); + + aXMLHttpRequest.onreadystatechange = function (aEvt) { + if(aXMLHttpRequest.readyState == 4){ + allData = aXMLHttpRequest.responseText; + if(allData === "noauth"){ + alert("Your session has expired. Relogin to continue.") + window.location.reload(); + } + callback(allData) + } + }; + aXMLHttpRequest.send(null); + } + else + { + alert("A problem occurred instantiating the XMLHttpRequest object."); + } +} + diff --git a/server.js b/server.js new file mode 100644 index 0000000..3d2a1e1 --- /dev/null +++ b/server.js @@ -0,0 +1,60 @@ +/** + * Created by Aloma on 10/18/2016. + */ +var express = require('express'); +var app = express(); +var ejs = require('ejs'); +var mysql = require('mysql'); +var bodyParser = require('body-parser'); +app.use(bodyParser.urlencoded({ extended: true })); +var HOST = 'localhost'; +var port = 3360; +var MYSQL_USER = 'root'; +var MYSQL_PASS = 'root'; +var DATABASE = 'articles'; +var TABLE = 'article_table'; + +app.get("/", function (req, res) { + res.redirect("/index.html"); +}); + +var connection = mysql.createConnection({ + host: HOST, + user: MYSQL_USER, + password: MYSQL_PASS, + database: DATABASE +}); + +connection.connect(function(err){ + if(!err) { + console.log("Database is connected ... \n\n"); + } else { + console.log("Error connecting database ... \n\n"); + } +}); + +app.get('/sendFormData', function(req, res) { + + var info = req.query; + console.log(info); + + res.send([]); + + /* FORM DATA STORED IN INFO + name = info.name + COMMENT = info.comment + + */ +// SQL COMMANDS HERE + var query = "Insert into "+TABLE+" (name,comment) VALUES ('"+info.name+"','"+info.comment+"')"; + connection.query(query,function(err, result) { + console.log(query.sql); + }); + +}); + +app.use(bodyParser()); +app.use(express.static(__dirname)); +console.log("Simple static server listening at http://:" + port); +app.listen(port); +