Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (42 loc) · 1.53 KB
/
Copy pathserver.js
File metadata and controls
59 lines (42 loc) · 1.53 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
// Setup all packages
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');
var config = require('./config');
var path = require('path');
// APP configuration
// Use bodyParser to grab info from POST requests
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Configure app to hangle CORS requests
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
next();
});
// Log all requests to the console
app.use(morgan('dev'));
// Connect to the db
mongoose.connect(config.database);
// set static files location
// used for requests that our frontend will make
app.use(express.static(__dirname + '/public'));
// API Routes
//===========================================
var apiRoutes = require('./routes/api')(app, express);
// REGISTER OUR ROUTES ----------------------------
// all routes will be prefixed with /api
app.use('/api', apiRoutes);
// Main catchall route
// Sends users to frontend
// has to be registered after apiRoutes
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/public/app/views/index.html'));
})
// START THE SERVER
//=================================================
app.listen(config.port);
console.log('Magic happens on port: ' + config.port);