-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
51 lines (41 loc) · 1.77 KB
/
server.js
File metadata and controls
51 lines (41 loc) · 1.77 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
var express = require('express');
var app = express();
var path = require('path');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var mongodbURL = require('./config').mongodb;
var CORShandler = require('./app/middleware').handleCORSrequests;
var authRoutes = require('./app/routes/auth');
var usersRoutes = require('./app/routes/users');
var studentsRoutes = require('./app/routes/students');
var parentsRoutes = require('./app/routes/parents');
// get our request parameters
//parse data in the form of x-www-form-urlencoded, http header: content-type: applicaion/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: true})); //result in an object, use JSON.stringify() to transform to string if needed
//parse data in the form of json, http header: content-type: applicaion/json
app.use(bodyParser.json()); //result in an object, use JSON.stringify() to transform to string if needed
// log client request info to console
//app.use(morgan('dev'));
app.use(express.static(__dirname + '/public'));
app.use(CORShandler);
mongoose.connect(mongodbURL);
//ROUTES
//public, unprotected routes
app.use('/api/authentication', authRoutes);
//protected routes that need to verify token
app.use('/api/users/', usersRoutes);
app.use('/api/students/',studentsRoutes);
app.use('/api/parents/',parentsRoutes);
//end of protected routes
// MAIN CATCHALL ROUTE ---------------
// SEND USERS TO FRONTEND ------------
// has to be registered after API ROUTES
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/public/app/views/index.html'));
});
//END OF ROUTES
var port = process.env.PORT || '8084';
app.listen(port, process.env.IP, function(){
console.log('CSOD web app is listening to ' + port);
});