-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (39 loc) · 1.17 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
const express = require('express');
const bodyParser = require('body-parser');
const firebase = require('./src/firebase');
const search = require('./app/search');
const dataImport = require('./app/import');
const port = process.env.PORT || 3000;
const app = express();
const router = express.Router();
// Setup dependencies
search.setDb(firebase);
dataImport.setDb(firebase);
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// ROUTES FOR OUR API
router.route('/slash')
.post((req, res) => {
if(req.body.text) {
search.run(req.body)
.then((response) => res.json(response));
} else {
res.send('There was an error with the request');
}
});
router.route('/import')
.get((req, res) => {
if(req.query.import && req.query.import === 'yoda') {
dataImport.run()
.then((response) => res.json(response));
} else {
res.send('There was an error with the request');
}
});
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
app.listen(port);