-
Notifications
You must be signed in to change notification settings - Fork 0
/
shuju.js
135 lines (117 loc) · 2.91 KB
/
shuju.js
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
var http = require("http");
var fs = require("fs");
var qs = require("querystring");
var util = require("util");
var mongodb = require("mongodb");
var mongoClient = mongodb.MongoClient;
var db_conn = "mongodb://localhost:27017/mation";
var webs = [];
var postData="";
var findData = "";
console.log("nishshi")
function insertData(db,callback){
var collection = db.collection("testOne");
collection.insert(postData,function(err,result){
callback(result);
})
};
function findDatadb(db,callback){
var collection = db.collection("testOne");
collection.find(findData).toArray(function(err, result) {
if(err)
{
console.log('Error:'+ err);
return;
}
callback(result);
})
};
var app = http.createServer(function(req,res){
//console.log("hello node.js");
//req.method
//req.url
if(req.method ==="GET"){
switch(req.url){
case "/index.html":
fs.readFile("index.html",function(err,data){
if(err) throw err;
res.writeHeader("200",{"Content-Type":"text/html"});
res.write(data.toString());
res.end();
})
break;
case "/add.html":
fs.readFile("add.html",function(err,data){
if(err) throw err;
res.writeHeader("200",{"Content-Type":"text/html"});
res.write(data.toString());
res.end();
})
break;
case "/find.html":
fs.readFile("find.html",function(err,data){
if(err) throw err;
res.writeHeader("200",{"Content-Type":"text/html"});
res.write(data.toString());
res.end();
})
break;
case "/remove.html":
fs.readFile("remove.html",function(err,data){
if(err) throw err;
res.writeHeader("200",{"Content-Type":"text/html"});
res.write(data.toString());
res.end();
})
break;
case "/edit.html":
fs.readFile("edit.html",function(err,data){
if(err) throw err;
res.writeHeader("200",{"Content-Type":"text/html"});
res.write(data.toString());
res.end();
})
break;
}
}else if(req.method ==="POST"){
switch(req.url){
case "/add.js":
req.on("data",function(chunck){
postData += chunck;
});
req.on("end",function(){
postData = qs.parse(postData)
//res.end(util.inspect(website));
webs.push(postData);
console.log(webs);
//webs是后台获取的数据
mongoClient.connect(db_conn,function(err,db){
//if(err) throw err;
console.log("success mongodb");
insertData(db,function(result){
console.log(result);
db.close();
})
})
res.end(util.inspect(postData));
})
break;
case "/find.js":
req.on("data",function(chunck){
findData += chunck;
});
req.on("end",function(){
findData = qs.parse(findData);
res.end();
mongoClient.connect(db_conn,function(err,db){
console.log("连接成功 可以查询");
findDatadb(db,function(db,result){
console.log(result);
db.close();
})
})
})
break;
}
}
}).listen(8080);