-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
64 lines (54 loc) · 1.59 KB
/
server.js
File metadata and controls
64 lines (54 loc) · 1.59 KB
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
const polka = require('polka');
//require('dotenv').config();
const PORT = process.env.PORT || 5000;
const uriMongo = process.env.DRIVER;
const MongoClient = require('mongodb').MongoClient;
let mongodb;
MongoClient.connect(uriMongo, {
useNewUrlParser: true,
useUnifiedTopology: true
}, (err, database) => {
if (err) return console.error(err);
console.log('Connected to Database');
mongodb = database;
});
const app = polka();
const HEADERS = {
'Content-Type': 'application/json',
'Access-Control-Allow-Credentials': "true",
"Access-Control-Allow-Origin": "*" ,
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version"
}
app.get('/', (req, res) => {
res.end('Hello world!');
});
app.get('/challenges/:pid', (req, res) => {
const data = mongodb.db('swat-mco');
const pid = req.params.pid
data.collection('challenges')
.findOne({
challengeId : parseInt(pid)
})
.then((result)=>{
res.writeHead(200, HEADERS)
res.end(JSON.stringify(result))
})
.catch(err => console.log(err))
});
app.get('/challenges', (req, res) => {
const data = mongodb.db('swat-mco');
data.collection('challenges')
.find({})
.toArray()
.then((result)=>{
//console.log(result)
res.writeHead(200, HEADERS)
res.end(JSON.stringify(result))
})
.catch(err => console.log(err))
});
app.listen(PORT, err => {
if (err) throw err;
console.log(`> Running on localhost:${PORT}`);
});