-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (65 loc) · 2.09 KB
/
index.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
var express = require('express');
var mongoose = require('mongoose');
var cors = require('cors');
var bunyan = require('bunyan');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var mongodb_uri = 'mongodb://localhost:27017/vomadrid';
// Create express app
var app = express();
// Create log object
var log = bunyan.createLogger({name: "vomadrid", level: 'debug'});
// Allow cors
app.use(cors());
// Mongodb connect. TODO: Make it configurable!
mongoose.connect(mongodb_uri);
mongoose.set('debug', function (collectionName, method, query, doc, options) {
// do your own custom logging
log.debug({'collectionName': collectionName, 'method': method, 'query': query, 'doc': doc});
})
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error: '));
db.once('open', function(callback) {
log.info('Mongodb connected');
});
var movieSchema = mongoose.Schema({
spiders_used: [],
movie_id: String,
movie_date_added: String,
movie_title: String,
movie_original_title: String,
movie_runtime: Number,
movie_gender: String,
movie_plot: String,
movie_director: String,
movie_actors: String,
movie_country: String,
movie_rating: Number,
movie_poster: String,
movie_showtimes: []
});
var movie = mongoose.model('Movie', movieSchema);
// TODO: We should get all the data here using mongoose
app.get('/v2', function(req, res) {
movie.find(function(err, doc) {
log.info(doc);
res.send(doc);
});
});
// Using mongodb driver, it works
app.get('/v1', function(req, res) {
MongoClient.connect(mongodb_uri, function(err, db) {
assert.equal(err, null);
log.info('Mongodb connected');
var docsArray = db.collection('vomovies').find().toArray().then(
function(data) {
res.send(data);
},
function(error) {
res.send('Error getting data!');
});
})
});
var server = app.listen(3000, function() {
console.log('Server running at http://localhost:' + server.address().port);
});