-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
53 lines (46 loc) · 1.58 KB
/
api.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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var MongoClient = require('mongodb').MongoClient;
var util = require('util');
var registro1, registro2;
// Connect to the db
MongoClient.connect("mongodb://localhost/meuBancoTeste", function(err, db) {
if(err) { return console.dir(err); }
registro1 = db.collection('registro1');
registro2 = db.collection('registro2');
});
app.use("/css", express.static(__dirname + '/css'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/img", express.static(__dirname + '/img'));
app.use("/fonts", express.static(__dirname + '/fonts'));
app.use("/font-awesome/css/", express.static(__dirname + '/font-awesome/css/'));
app.get('/', function (req, res) {
res.sendfile('index.html');
});
app.get('/collection1/', function(req,res){
registro1.find().toArray(function(err, items) {
res.send(items);
});
});
app.get('/collection1/:nome', function(req,res){
registro1.find({ "nome": req.params.nome }).toArray(function(err, items) {
console.log(req.params.nome);
res.send(items);
});
});
app.get('/collection2/', function(req,res){
registro2.find().toArray(function(err, items) {
res.send(items);
});
});
app.get('/collection2/:universidade', function(req,res){
registro2.find({"universidade": req.params.universidade}).toArray(function(err, items) {
console.log(req.params.universidade);
res.send(items);
});
});
http.listen(4000, function(){
console.log('Disponível na porta 4000');
});