-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
55 lines (47 loc) · 1.62 KB
/
app.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
const dotenv = require('dotenv').config()
const express = require('express')
const MongoClient = require('mongodb').MongoClient
//
// setup debug.log
//const fs = require('fs')
//const util = require('util')
//let logFile = fs.createWriteStream( './debug.log' )
//console.log = (message) => {
// message = util.format(message) + '\n'
// logFile.write(message)
//}
const app = express(); // activate an express app
app.use( express.json() ); // enable parsing of JSON data in our app
app.use('/DragonAPI', express.static('public'))
// Listen for HTTP GET requests.
app.get('/DragonAPI/dragons/', (req, res) => {
// log information about the request
console.log('Dragons requested'
+ ' on ' + new Date()
+ ' by ' + req.headers['x-forwarded-for']
+ ' with ' + req.headers['user-agent']
+ ' at ' + req.headers['referer']
)
const client = new MongoClient(
process.env.DB, {
useUnifiedTopology: true,
useNewUrlParser: true
}
);
client.connect(err => {
if (err) { console.log(err); }
let query = {height:{$gt:10}}
let projection = {'name':1,'weight':1,'height':1, 'sprites':1,'_id':0}
// search mongo collection for all dragons
client.db("Dragons").collection("Pokemon")
.find( query )
.project( projection )
.toArray( (err, item) => {
if (err) { res.send({ 'error': 'An error has occured' }); }
else { res.send( item ); } // send the result back.
});
});
});
app.listen(6000, "localhost", () => {
console.log("We are live." );
});