diff options
Diffstat (limited to 'csci4131/hw5/strap012_hw5')
-rwxr-xr-x | csci4131/hw5/strap012_hw5/client/contacts.html | 39 | ||||
-rwxr-xr-x | csci4131/hw5/strap012_hw5/createServer.js | 13 | ||||
-rwxr-xr-x | csci4131/hw5/strap012_hw5/package.json | 1 |
3 files changed, 47 insertions, 6 deletions
diff --git a/csci4131/hw5/strap012_hw5/client/contacts.html b/csci4131/hw5/strap012_hw5/client/contacts.html index e24ca84..2e4bf65 100755 --- a/csci4131/hw5/strap012_hw5/client/contacts.html +++ b/csci4131/hw5/strap012_hw5/client/contacts.html @@ -5,6 +5,41 @@ <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>
+ <script type="text/javascript" defer>
+ //Get JSON
+ var xmlhttp = new XMLHttpRequest();
+ var url = "contacts.json";
+ xmlhttp.onreadystatechange = function () {
+ if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
+ var parse = JSON.parse(xmlhttp.responseText);
+ process(parse);
+ }
+ }
+ xmlhttp.open("GET", url, true);
+ xmlhttp.send();
+
+ function process(a) {
+ var table = document.getElementsByTagName("tbody")[0];
+ var contacts = a.contacts;
+ for (let i = 0; i < contacts.length; i++) {
+ var contact = contacts[i];
+ var row = table.insertRow(-1);
+ var nam = row.insertCell(0);
+ nam.innerHTML = contact.name;
+ var cat = row.insertCell(1);
+ cat.innerHTML = contact.category;
+ var loc = row.insertCell(2);
+ loc.innerHTML = contact.location;
+ var con = row.insertCell(3);
+ con.innerHTML = contact.contact;
+ var ema = row.insertCell(4);
+ ema.innerHTML = contact.email;
+ var url = row.insertCell(5);
+ url.innerHTML = "<a href =" + contact.website_url + ">" + contact.website_url + "</a>";
+ }
+ }
+
+ </script>
</head>
<body>
<nav class="navbar navbar-default">
@@ -35,9 +70,7 @@ </table>
</div>
<script type="text/javascript">
- /* TODO Fill this script in with a request to your server to GET contacts.json
- / and display it in the contactsTable.
- /*/
+
</script>
</body>
</html>
diff --git a/csci4131/hw5/strap012_hw5/createServer.js b/csci4131/hw5/strap012_hw5/createServer.js index 01f1f08..8f79c9f 100755 --- a/csci4131/hw5/strap012_hw5/createServer.js +++ b/csci4131/hw5/strap012_hw5/createServer.js @@ -9,7 +9,8 @@ http.createServer(function (req, res) { var q = url.parse(req.url, true);
var filename = "." + q.pathname;
if(req.url === '/'){
- indexPage(req,res);
+ res.writeHead(302, { 'Location': 'contacts.html' });
+ return res.end("302 Temporary Redirect");
}
else if (req.url === '/index.html') {
indexPage(req, res, '/index.html');
@@ -21,7 +22,13 @@ http.createServer(function (req, res) { indexPage(req, res, '/addContact.html');
}
else if (req.url === '/stock.html') {
- jsonSock(req, res, '/stock.html');
+ indexPage(req, res, '/stock.html');
+ }
+ else if (req.url === '/contacts.json') {
+ jsonSock(req, res, 'contacts.json')
+ }
+ else if (req.url === "/postContactEntry") {
+
}
else{
res.writeHead(404, {'Content-Type': 'text/html'});
@@ -43,7 +50,7 @@ function indexPage(req, res, file) { }
function jsonSock(req, res, file) {
- fs.readFile('client/' + file, function (err, json) {
+ fs.readFile(file, function (err, json) {
if (err) {
throw err;
}
diff --git a/csci4131/hw5/strap012_hw5/package.json b/csci4131/hw5/strap012_hw5/package.json index 90f2542..1253c2a 100755 --- a/csci4131/hw5/strap012_hw5/package.json +++ b/csci4131/hw5/strap012_hw5/package.json @@ -4,6 +4,7 @@ "description": "Assignment 5", "main": "createServer.js", "scripts": { + "start": "node createServer.js", "test": "node createServer.js" }, "author": "strap012", |