-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (79 loc) · 2.83 KB
/
server.js
File metadata and controls
102 lines (79 loc) · 2.83 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
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
98
99
100
101
102
// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var cors = require('cors');
var apiController = require('./controllers/api');
var http = require('http');
var fs = require('fs');
var path = require("path");
var bootstrap = require("express-bootstrap-service");
var d3 = require("d3");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// app.use(express.static('public'));
app.use(express.static(path.join(__dirname, 'views')));
app.use(bootstrap.serve);
//Enable All CORS Requests
app.use(cors());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
//
router.get("/",function(req,res){
res.sendFile('index.html');
});
//
// router.get('/the_science',function(req,res){
// res.sendFile("the_science.html");
// });
//
// router.get('/getting_started',function(req,res){
// res.sendFile("getting_started.html");
// });
// endpoint
router.post('/v1/analyze', function(req, res) {
res.json({
message: req.body.text
});
});
// path on my app
router.post('/analyze', function (request, response, next) { // function is a callback and it wildo the thing
// console.log('this is the request', request);
console.log('got here', apiController);
// console.log('this is a body request', request.body);
apiController.getAnalysis(request.body, function (error, apiResponseobject, body) { // json always patterned as error, results
console.log('this is the body response', body);
// console.log('this is the error', error);
if (error) { response.status(500).json({ error: error.message }); }
// console.log('reached success!');
response.json(JSON.parse(body.body));
});
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /
app.use('/', router);
// app.use("*",function(req,res){
// res.sendFile(path + "404.html");
// });
// print the type of http request that route is referring to
router.use(function (req,res,next) {
console.log("/" + req.method);
next();
});
// START THE SERVER
// =============================================================================
// Setup HTTPS
// var options = {
// key: fs.readFileSync('./private.key'),
// cert: fs.readFileSync('./certificate.pem')
// };
app.listen(port);
// var secureServer = http.createServer(options, app).listen(port);
console.log('Magic happens on port ' + port);
module.exports = router;