forked from BYU-IT515R-Javascript-W15/wd_example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (107 loc) · 3.19 KB
/
app.js
File metadata and controls
124 lines (107 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var express = require('express');
var app = express();
var path = require("path");
var cheerio = require("cheerio");
var fs = require("fs");
var bodyParser = require('body-parser');
var mysql = require('mysql');
app.use(bodyParser.urlencoded());
app.get("/test", function (req, res) {
$ = get_page('magic_page.html');
$('#magic').text('Here is some new text!!!');
res.send($.html())
})
app.get("/login", function (req, res) {
res.sendFile(path.join(__dirname + "/login.html"));
});
app.get("/home", function (req, res) {
var username = req.query.name;
var passwd = req.query.password;
if (username != undefined && passwd != undefined) {
if (username != "collin" || passwd != 'collin') {
var thealert = "alert('INCORRECT NAME OR PASSWORD! THE HOUNDS HAVE BEEN RELEASED!')";
}
}
$ = get_page('home.html')
res.send($.html());
});
app.get("/form1", function (req, res) {
var input = req.query.kevin;
if (input == undefined) {
input = '';
}
$ = get_page('form1.html');
$('#val').text(input);
$('#textbox').val(input);
res.send($.html());
//res.send("<html> <body><a href='home'>Home</a> <h1> Entered value: " + input + "</h1><form action='' method='get'><input type='text' name='kevin' value='" + input + "'><input type='submit' value='Change Value'></form> </body> </html>")
});
app.get("/magic",function(req,res){
$=get_page('magic_page.html')
var query=req.query;
var target=$('body')
/*if(query.color){
target.css('color',query.color);
}*/
for(key in query){
target.css(key,query[key])
}
res.send($.html());
});
app.post("/form2", function (req, res) {
var postData = req.body;
var $ = get_page('form2_results.html');
$('#results').text('Result: '+postData.entry_text+'');
res.send($.html());
});
app.get("/form2", function (req, res) {
$ = get_page('form2.html');
res.send($.html());
});
app.get('/json',function(req,res){
$=get_page('json.html');
var jsonfile=fs.readFileSync('employees.json');
var thejson=JSON.parse(jsonfile);
for(var i=0;i<thejson.length;i++){
var entry=thejson[i];
$('ul').append('<li>'+entry.firstName+' '+entry.lastName+'</li>');
}
res.send($.html());
});
app.get("/form3", function (req, res) {
res.send("<html><body></body></html>")
});
app.listen(8088, function () {
});
function get_page(filepath) {
var thefile=fs.readFileSync(filepath);
return cheerio.load(thefile);
}
/*function db_connect() {
return mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'js'
});
}
function db_test() {
//var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'js'
});
connection.connect();
connection.query('SELECT * from entries', function (err, rows, fields) {
if (!err) {
rows.forEach(function (entryRow) {
console.log(entryRow.entry)
})
}
else
console.log('Error while performing Query.');
});
connection.end();
}*/