-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-formContacto.js
65 lines (47 loc) · 1.42 KB
/
server-formContacto.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
var fs=require("fs");
var qs = require("querystring");
var config=JSON.parse(fs.readFileSync("config.json"));
var host=config.host;
var port=config.port;
var exp=require("express");
var app=exp(); //el tutorial indicaba exp.createServer()
//app.use(app.router);
app.use('/',exp.static(__dirname)); //+ "/formContactoMongo"));
var contactoCol;
app.get("/",function(request,response){
var contenido=fs.readFileSync("./ajax30.html");
response.setHeader("Content-type","text/html");
response.send(contenido);
});
app.post("/peticion",function(request,response){
var datos='';
request.on('data', function(chunk) {
datos+=chunk;
});
request.on('end', function() {
var cont=qs.parse(datos);
contactoCol.insert(cont,function(error){
if(error){
console.log("Hubo un error");
}
else{
console.log("Elemento insertado");
}
});
console.log(cont);
response.end();
});
});
console.log("servidor iniciado...");
app.listen(port,host);
var mongo=require("mongodb");
var hostDB="127.0.0.1";
var portDB=mongo.Connection.DEFAULT_PORT;
var db=new mongo.Db("contactosDB",new mongo.Server(hostDB,portDB,{}));
db.open(function(error){
console.log("Conectado a la base de datos "+host+" "+port);
db.collection("contactos",function(error,col){
console.log("Tenemos la colección");
contactoCol=col;
});
});