-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserver.js
65 lines (52 loc) · 1.48 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
// sets up server requirements
var express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
//require database
var db = require('./DB/db.js');
//creates server
var app = express();
//creates default port
var port = process.env.PORT || 4444;
//middleware
app.use(express.static(__dirname + '/app'));
console.log("Hello")
app.use(bodyParser.json());
// %%%%%%%%%%%This Should Fix our CORS%%%%%%%%%%%%%%%%%%%%
app.use(function(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
next();
});
// %%%%%%%%%%%%%^^^^^^^^^^^^^^^%%%%%%%%%%%%%%%%%%%%%%%%%%%
//mongoose connection
mongoose.connect('mongodb://indigozone:[email protected]:15962/heroku_v75xg8cs');
//connects app and port
app.listen(port);
//handles register
app.post('/api/register', function(req,res){
db.create({
username: req.body.username,
password: req.body.password
}, function(err, user){
res.send(user);
});
});
//handles login
app.post('/api/login', function(req,res){
db.findOne({username:req.body.username}, function(err, user){
if (err){
res.send(err);
} else if(user){
if(req.body.password === user.password){
res.send('success');
}
} else {
res.send('failure');
}
});
});
//prints sucess when the server is running
console.log('Server now listening on port: ', port);
//exports the app server
module.exports = app;