-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverNode.js
53 lines (43 loc) · 2.06 KB
/
serverNode.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
// load the express package and create our app
// REQUIRE PACKAGES --------------------------
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser'); // get body-parser
var morgan = require('morgan'); // used to see requests
var mongoose = require('mongoose'); // for working w/ our database
var config = require('./config');
var path = require('path');
// var superSecret = 'ilovescotchscotchyscotchscotch'; // JWT secret
// APP CONFIGURATION --------------------------
// use body parser so we can grab information from POST requests
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// configure our app to handle CORS requests (Cross-Origin Resource Sharing)
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'));
// MongoDB connect
// mongoose.connect('mongodb://localhost/test'); // local test db
mongoose.connect('mongodb://node:[email protected]:27017/Iganiq8o');
// mongoose.connect('mongodb://leongaban:[email protected]:10016/sandbox');
// API ROUTES ------------------------
var apiRoutes = require('./app/routes/api')(app, express);
app.use('/api', apiRoutes);
var adminRouter = require('./app/routes/api')(app, express);
app.use('/admin', adminRouter);
// MAIN CATCHALL ROUTE ---------------
// SEND USERS TO FRONTEND ------------
// has to be registered after API ROUTES
app.use(express.static(__dirname + '/public'));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/public/views/index.html'));
});
// START THE SERVER
// =============================================================================
app.listen(config.port);
console.log('••• Magic happens on port ' + config.port + '! •••');