-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (73 loc) · 2.14 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
require('dotenv').config();
const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const port = process.env.PORT || 4000;
const mongoose = require('mongoose');
const session = require('express-session');
const cookieParser = require('cookie-parser');
const history = require('connect-history-api-fallback');
// use cors for cross-origin accessibility
app.use(cors());
//body-parser middleware
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}));
// parse application/json
app.use(bodyParser.json());
// session
app.use(cookieParser());
app.use(session({
secret: `${process.env.secret}`,
resave: true,
saveUninitialized: false,
cookie: {
expires: false,
},
// session: true
}));
//Passport
app.use(passport.initialize());
app.use(passport.session());
// calling passport strategy function
require('./config/passport')(passport);
app.use(express.static(path.join(__dirname, '/dist/web-challenge')));
// app.use('/',path.join(__dirname,'/dist/mean/index.html'));
// for fallback and relaoding to work
app.use(history());
// connecting mongo
mongoose.Promise = global.Promise;
mongoose.connect(process.env.mongoUri, {
useNewUrlParser: true
})
.then(() => {
console.log('connected mongo');
})
.catch((err) => {
console.log(err);
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/dist/web-challenge/index.html'));
});
// test routes
app.get('/test', (req, res) => {
res.send('test works!!');
});
// users routes
const users = require('./server/users');
app.use('/users', users);
const products = require('./server/product');
app.use('/products', products);
// for all other routes
app.get('**', (req, res) => {
res.sendFile(path.join(__dirname, './dist/web-challenge/index.html'));
});
const server = app.listen(port, () => {
console.log(`listening on ${port}`);
});
module.exports = server;
// https: //auctioner.herokuapp.com/allProducts