-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (52 loc) · 1.34 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
const express = require('express');
const fileUpload = require('express-fileupload');
const app = express();
const mongodb = require('mongodb');
const uploadFile = require('./uploadfile.js').uploadFile;
const config = {
DB: 'mongodb://mongo'
};
const optiondDB = {
useNewUrlParser: true
};
const PORT = 3000;
const client = mongodb.MongoClient;
var dbo;
client.connect(config.DB, optiondDB, function(err, db) {
if (err) {
console.log('database is not connected')
} else {
console.log('connected!!');
dbo = db.db('midb');
}
});
app.use(fileUpload());
app.get('/', function(req, res) {
res.json({"hello": "express with mongo"});
});
app.get('/pokedex', function(req, res) {
let data = dbo.collection('pokedex').find({}).toArray((err, result) => {
if (err) {
throw err;
}
res.json(result);
});
});
app.delete('/pokedex/:id', function(req, res) {
dbo.collection('pokedex').find({"id":req.params.id}).toArray((err, result) => {
if (err) {
throw err;
}
console.log(result);
dbo.collection('pokedex').deleteOne({"_id":result._id}, function(err, obj) {
if (err) {
throw err;
}
console.log("document with id " + req.params.id + " deleted");
res.json(obj);
});
})
});
app.listen(PORT, function() {
console.log('Your node js server is running on PORT:', PORT);
});