-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
60 lines (50 loc) · 1.71 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
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
validator = require('express-validator'),
cookieParser = require('cookie-parser'),
cors = require('cors'),
mongoose = require('mongoose'),
session = require('express-session'),
passport = require('passport'),
mongoStore= require('connect-mongo')(session),
config = require('./config/database'),
userroutes = require('./routes/user-routes'),
PORT = 3000; //PORT
mongoose.set('debug', true);
//mongoose connectio and connection check
mongoose.connect(config.database);
mongoose.connection.on('connected',()=>{
console.log('connected to' + config.database );
});
mongoose.connection.on('error', ()=>{
console.log('not connected to' +config.database);
});
//Middlewares
const app = express();
app.use(bodyParser.json());
app.use(validator()); //important - after body parser
app.use(cookieParser());
app.use(cors());
require('./config/passport')(passport), //important (passport)
app.use(session({
secret : 'mysupersecret',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
//Routes
app.use('/users', userroutes);
//statics
app.use(express.static(path.join(__dirname+'/client')));
app.get('/',(req,res)=>{
res.send('invalid endpoint');
});
app.get('*', (req,res) => {
res.sendFile(path.join(__dirname, 'client/index.html'));
})
//listen
app.listen(PORT,(req, res)=>{
console.log('listening');
})