-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (51 loc) · 1.84 KB
/
app.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
59
60
61
62
63
64
65
66
67
68
69
70
var express = require('express'),
bodyParser = require('body-parser'),
passport = require('passport'),
session = require('express-session'),
config = require('config');
var app = express();
//Set up body-parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//Passport Config
require('./config/passport')(passport);
app.use(session({secret : 'app secreet', resave: true, saveUninitialized: false}));
app.use(passport.initialize());
app.use(passport.session());
//apply user object to res if there is a user object
app.get('*', function(req, res, next){
res.locals.user = req.user || null;
next();
});
//reply with whether or not the user is logged in
app.get('/loggedin', function(req, res){
res.status(req.isAuthenticated() ? 200 : 401);
res.send(req.isAuthenticated() ? req.user : '0');
});
//set up routes for application
app.use('/', express.static(__dirname + '/public/'));
app.use('/scripts', express.static(__dirname + '/node_modules/'));
//web page route
app.get('/', function(req, res) {
res.sendFile('./public/index.html', {"root": __dirname});
});
//Set up mongoose. Will be moved to another location later
var mongoose = require('mongoose');
var dbUri = config.db;
mongoose.connect(dbUri, { useNewUrlParser: true});
mongoose.Promise = global.Promise;
const db = mongoose.connection;
db.on('error', console.error.bind(console,'MongoDB connection error: '));
db.once('open', function(){
console.log("Connected to mongodb at: " + dbUri);
});
//API routes
let users = require('./backend/api/usersRoutes');
let orders = require('./backend/api/ordersRoutes');
app.use('/api/users', users);
app.use('/api/orders', orders);
//start app listening
const port = process.env.PORT || 8080;
app.listen(port);
console.log("App Listening on port: " + port);
module.exports = app;