-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailDB.js
More file actions
138 lines (133 loc) · 3.52 KB
/
EmailDB.js
File metadata and controls
138 lines (133 loc) · 3.52 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
var http = require('http');
//set up web use variables
var mongoose = require('mongoose');
//set up database
var fs = require('fs');
//read file command
mongoose.connect('mongodb://localhost/my_database');
//local mongodb connection
var schema = new mongoose.Schema({
from: 'string',
to: 'string',
name: 'string',
title: 'string',
body: 'string'
});
//schema is the model of mongoose, set up runs through entire coding
var Email = mongoose.model('Email', schema);
var emailArray = new Array();
for (var i = 0; i < 10; i++) {
var email = new Email();
email["from"] = 'Joy' + i.toString() + '@gmail.com';
email["to"] = 'Ling' + i.toString() + '@gmail.com';
email["name"] = 'Ling' + i.toString();
email["title"] = 'Greeting' + i.toString();
email["body"] = i.toString() + 'How are you?';
emailArray.push(email);
}
console.log('Email' + emailArray);
for (var j = 0; j < 10; j++) {
emailArray[j].save(function(err) {
if (err) return console.error(err);
});
}
console.log('finished');
//delcare Email is a schema and generate data and push in to an array. The array saved in the database
//created a function name requestHandler, base on if and else commands
function requestHandler(req, res) {
debugger;
if (req.url === "/bootstrap.css") {
fs.readFile("bootstrap.css", {
encoding: "utf8"
}, function(err, data) {
res.writeHead(200, {
'Content-Type': 'text/css'
});
if (err) throw err;
res.write(data);
res.end();
//read file .css and display in webpage
});
} else if (req.url === "/index.html") {
fs.readFile("Emailvalid.html", {
endocding: "utf8"
}, function(err, data) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
if (err) throw err;
res.write(data);
res.end;
fs.readFile("indexheader.html", {
endocding: "utf8"
}, function(err, data) {
res.write('<h1>Email HomePage</h1>');
Email.find({
name: 'Ling8'
}, function(err, found) {
if (err) return console.error(err);
console.log('err check');
if (found) {
for (var k = 0; k < found.length; k++) {
res.write("<h2>" + "From: " + found[k].from + "</h2>");
res.write("<h2>" + "Body: " + found[k].body + "</h2>");
}
};
fs.readFile("indexfooter.html", {
endocding: "utf8"
}, function(err, data) {
res.end("<h1>Finished my email test</h1>");
});
});
});
});
} else if (req.url === "/bootstrap.js") {
fs.readFile("bootstrap.js", {
endcoding: "utf8"
}, function(err, data) {
res.writeHead(200, {
'Content-Type': 'text/js'
});
if (err) throw err;
res.write(data);
res.end();
});
} else
if (req.url === "/panels.html") {
res.writeHead(404, {
'Content-Type': 'text/html'
});
res.write('<h1>404, webpage not found</h1>');
res.end();
} else if (req.url === "/action") {
req.on('data', function(chunk) {
console.log("Received body data:");
console.log(chunk.toString());
var inputEDB = chunk.toString().match(/Address=(.*)/);
console.log(inputEDB);
inputEDB.save(function(err){
if (err) return console.error(err);
});
});
req.on('end', function() {
// empty 200 OK response for now
res.writeHead(200, "OK", {
'Content-Type': 'text/html'
});
res.end();
});
} else {
fs.readFile("panels.html", {
encoding: "utf8"
}, function(err, data) {
res.writeHead(200, {
'Content-Type': 'text/html'
});
if (err) throw err;
res.write(data);
res.end();
});
}
}
http.createServer(requestHandler).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');