-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodetutorials.js
More file actions
167 lines (100 loc) · 3.14 KB
/
nodetutorials.js
File metadata and controls
167 lines (100 loc) · 3.14 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Created by Anastasia on 2/19/2016.
*/
//var app = require('express')();
//var http = require('http').Server(app);
var fs = require('fs');
//var io = require('socket.io')(http);
var io = require('socket.io');
v
var path = require('path');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var express = require('express');
var app = express();
var sql = require('mysql');
var querystring = require('querystring');
var databasecon = sql.createConnection({
host: 'mysql.cs.iastate.edu',
user: 'dbu309grp43',
password: 'zGtT4R5s5fR',
database: 'db309grp43'
});
databasecon.connect(function (err) {
if (err) {
console.log('Error connecting to Db');
return;
}
console.log('Connection established');
});
databasecon.query('SELECT * FROM Students', function (err, rows) {
if (err) throw err;
console.log('Data received from Db:\n');
//console.log(rows);
fs.writeFile('table.json', JSON.stringify(rows), function (err) {
if (err) throw err;
console.log('Saved!');
});
});
app.get("/goodbye", function (req, res) {
//send "Goodbye World" to the client as html
res.send("Goodbye World!");
});
app.get('/', function(req, res){
if (req.method=='GET'&&req.url=='/'){
/*
res.writeHead(200,{"Content-Type":"text/html"});
fs.createReadStream("./CyShapes.html").pipe(res);
var contents = fs.readFileSync("table.json");
var jsonContent = JSON.parse(contents);
*/
res.writeHead(200,{"Content-Type":"text/html"});
fs.createReadStream("./CyShapes.html").pipe(res);
}
});
var usersFilePath = path.join(__dirname, 'table.json');
app.get('/users', function (req, res) {
// Prepare output in JSON format
var readable = fs.createReadStream(usersFilePath);
readable.pipe(res);
});
app.use(express.static('public'));
app.get('/LoginPage', function (req, res) {
res.sendFile( __dirname + "/" + "LoginPage.html" );
})
app.get('/process_get', urlencodedParser, function (req, res) {
// Prepare output in JSON format
var response = {
username:req.body.user_name,
password:req.body.password
};
console.log(response);
res.end(JSON.stringify(response));
})
/*
var listener = io.listen(http);
listener.sockets.on('connection', function(socket,res) {
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('client_data', function(data){
process.stdout.write(data.letter);
console.log(data.letter);
});
socket.on('CH01', function (from, msg) {
console.log('MSG', from, ' saying ', msg);
});
});
var socket = io.connect;
*/
/*
http.listen(3000, function(){
console.log('listening on *:3000');
});
*/
var server = app.listen(3000, function (socket) {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port);
});