-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
58 lines (46 loc) · 1.54 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
// DEPENDENCIES
// ==============================================
var express = require('express');
var app = express();
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var path = require('path');
var morgan = require('morgan');
var passport = require('passport');
var config = require('./config');
// CONFIGURATION
// ==============================================
// Connect to DB
var db = config.database;
mongoose.connect(db);
// BodyParser for POST requests
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Handle CORS
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'));
// Static files location
app.use(express.static(__dirname + '/public'));
// Initialize Passport!!! (This only wasted 2h of my time)
app.use(passport.initialize());
// ROUTER
// ==============================================
// API
var apiRouter = require('./app/routes/api.js')(app, express);
app.use('/api/v1', apiRouter);
var authRouter = require('./app/routes/auth.js')(express);
app.use('/authenticate', authRouter);
// Front End
app.get('*', function(req, res) {
res.send('Welcome to the front end');
});
// START SERVER
// ==============================================
app.listen(config.port);
console.log('Serving beer on port ' + config.port);