-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathejemplo2-mongo-server.js
66 lines (52 loc) · 1.44 KB
/
ejemplo2-mongo-server.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
//instalar express en este ejemplo si no lo teníamos
var fs=require("fs");
var config=JSON.parse(fs.readFileSync("config.json"));
var host=config.host;
var port=config.port;
var exp=require("express");
//incluimos la parte de MongoDB
var mongo=require("mongodb");
var dbHost="127.0.0.1";
var dbPort=mongo.Connection.DEFAULT_PORT;
var app=exp(); //el tutorial indicaba exp.createServer()
app.use(app.router);
app.use(exp.static(__dirname + "/public"));
app.get("/",function(request,response){
response.send("hola");
});
app.get("/hola/:text",function(request,response){
response.send("Hola "+request.params.text);
});
app.get("/user/:id",function(request,response){
getUser(request.params.id,function(user){
if(!user){
response.send("usuario no existe",404);
}
else{
response.send("Usuario: "+user.name+" cuenta: "+user.cuenta);
}
})
})
app.get("*",function(request,response){
response.send("Oh, no ",404);
})
app.listen(port,host);
function getUser(id,callback){
var db=new mongo.Db("nodejs-introduction",new mongo.Server(dbHost,dbPort,{}));
db.open(function(error){
console.log("Conectado a la base de datos "+dbHost+" "+dbPort);
db.collection("user",function(error,col){
console.log("Tenemos la colección");
col.find({"id":id.toString()},function(error,cursor){
cursor.toArray(function(error,users){
if (users.length==0){
callback(false);
}
else{
callback(users[0]);
}
})
})
})
})
}