-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
50 lines (36 loc) · 1.74 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
// REQUIRED -------------------------------------------------------------------
var express = require('express');
var bodyParser = require('body-parser')
var cors = require('./services/cors');
// CONFIG ---------------------------------------------------------------------
var app = express();
app.use(bodyParser.json()); // Specifies the format to parse
// Enabling CORS (comunicate with API on a different URL)
app.use(cors);
// MONGO (mongoose) -----------------------------------------------------------
//var mongo = require('mongodb').MongoClient; STOCK MONGO
var mongoose = require('mongoose');
var database;
// 27017 = DB port, test = DB name
//mongo.connect("mongodb://localhost:27017/test", function(err, db){ STOCK MONGO
mongoose.connect("mongodb://localhost:27017/test", function(err, db){
if(err){
console.log("problems connecting to DB");
console.log("run C:\Program Files\MongoDB\Server\3.2\bin\mongod.exe");
return;
}
console.log("we are connected to mongo");
// database = db; STOCK MONGO
})
// API ------------------------------------------------------------------------
var auth = require('./controllers/auth');
app.post('/auth/register', auth.register);
app.post('/auth/login', auth.login);
var message = require('./controllers/message');
var checkAuthenticated = require('./services/checkAuthenticated');
app.post('/api/message', checkAuthenticated, message.post);
app.get('/api/message', message.get); // Pass a fuction directly
// OTHER ----------------------------------------------------------------------
var server = app.listen(5000, function(){
console.log('listening on port ', server.address().port);
})