-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
97 lines (90 loc) · 2.71 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
85
86
87
88
89
90
91
92
93
94
95
96
97
const express = require('express');
const dotenv = require('dotenv');
const MongoClient = require('mongodb');
const pino = require("pino");
const bodyParser = require('body-parser')
dotenv.config();
const app = express();
app.use( bodyParser.json() ); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
// Add headers
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', process.env.SITE_URL);
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,X-CSRFToken');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
const logger = pino();
const server = app.listen(process.env.SERVER_PORT, () => {
logger.info(`cifrabox-chords listening on port ${process.env.SERVER_PORT}`);
});
var connection = null;
var collection = null;
MongoClient.connect(process.env.MONGO_URI, (err, database) => {
if(err == null)
console.log("Connected correctly to server");
else
console.log(err)
connection = database;
collection = database.collection('chords');
collection.drop()
if(!collection){
console.log("Collection is null");
connection.createCollection('chords', (err, col) => {
if(err == null)
console.log("Collection created successfully")
collection = col;
populateDatabase(()=>{
listenRoute()
})
});
}else {
collection.count(function(err, count) {
if(count > 0){
listenRoute()
} else {
populateDatabase(()=>{
listenRoute()
})
}
});
}
})
function populateDatabase(cb, index=0){
const arq = require("./chords/results.json")
arq.forEach((obj, i)=>{
collection.insertOne(obj, (err, r) => {
if(err) console.log(err)
});
});
console.log("Finished import results.json");
cb()
}
function listenRoute(){
app.get('/api/chords/v1', function (req, res) {
var chord = req.query['chord']
if(chord){
chord = chord.replace(/(\d)\+/g,'Maj$1')
chord = chord.replace(/(\d)\M/g,'Maj$1')
chord = chord.replace(/º/g, '°')
collection.findOne({name:chord},function(err, docs) {
res.send(docs)
});
}else{
res.send({detail: 'Chord invalid'})
}
})
app.get('/api/chords', (req, res)=>{
console.log("worked")
res.json({"detail":"successfully created"})
});
}