-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
129 lines (103 loc) · 3.15 KB
/
server.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Imported modules
//============================================================
//npm server package
var express = require('express');
//npm package used for
var bodyParser = require('body-parser');
//npm package to handle file pathways
var path = require('path');
//module to establish mysql connection with Jaws DB
var connection = require('./config/connection.js');
//npm module 'dot-env'; deal with sensitive info
require('dotenv').config();
var methodOverride = require('method-override');
var axios = require("axios");
//Run express app
//================================================
var app = express();
var PORT = process.env.PORT || 3000;
//App listener
app.listen(PORT, function(){
console.log('Listening on port: ' + PORT);
});
// Serve static files
app.use(express.static(__dirname + 'public/build'));
//Express middleware for parsing info for http POST requests
//================================================
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.text());
app.use(bodyParser.json({type:'application/vnd.api+json'}));
// establish connection to mysql/jawsdb
connection.connect(function(err){
if (err){
console.error(err.stack);
}
console.log('connected as ID ' + connection.threadId);
});
//Get route for the profile page
//================================================
var mongojs = require('mongojs');
//in databaseUrl: remove angled brackets
var databaseUrl = 'mongodb://pro:[email protected]:17819/heroku_54q3r8f2';
var collections = ['sentences'];
var db = mongojs(databaseUrl, collections);
db.on('error', function(err){
console.log('Database error: ', err);
});
app.get('/all', function(req, res){
db.sentences.find({}, function(err, found){
if (err) console.log(err);
else {
res.json(found);
console.log(found.length);
}
});
})
// app.get('/', function(req, res){
// res.send('./index.html');
// });
app.get('/', function(req,res) {
res.sendFile(path.resolve(__dirname, 'public/build/index.html'));
});
// app.get('/css/:name', function(req, res) {
// var fileName = req.params.name;
// var options = {
// root: __dirname + './../public/css/',
// dotfiles: 'deny',
// headers: {
// 'x-timestamp': Date.now(),
// 'x-sent': true
// }
// };
app.post('/addsentence/', function(req, res){
var userinput = req.params.body;
console.log(userinput);
res.json('good job, sonnnnnn!');
// db.sentences.insert({
// "user_id": "contributor2",
// "original": userinput,
// "revised": []
// });
res.redirect('/all');
});
app.get('/randomsentence', function(req, res){
var databaseSize = null;
db.sentences.find({}, function(err, found){
if (err) console.log(err);
else {
// res.json(found);
// console.log(found.length);
var databaseSize = found.length;
var random = Math.floor(Math.random() * databaseSize);
console.log(random);
res.json(found[random]);
}
});
});
//External routing files
//================================================
// require('./api/api-routes.js')(app);
// require('./api/admin-routes.js')(app);
// require('./api/login-register-routes.js')(app);
// require('./api/static-file-routes.js')(app);