-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (55 loc) · 1.75 KB
/
server.js
File metadata and controls
76 lines (55 loc) · 1.75 KB
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
const express = require('express'),
path=require('path'),
config=require('./config/database')
mongoose=require('mongoose'),
bodyParser=require('body-parser'),
passport=require('passport'),
cors=require('cors'),
options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } },
replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };
//const port=3000;
const port = process.env.PORT||8080;
//User
const users=require('./routes/users');
//Express
const app=express();
//body parser middleware : grab data from form etc
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
// allow cors
app.use(cors());
//static path : public -- client side, Angular App
app.use(express.static(path.join(__dirname,'public')));
//passport middleware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
app.use('/users',users);
//Index Route
app.get('/',function(req,res){
res.send('Invalid Endpoint');
});
//promises error
mongoose.Promise = global.Promise;
//MongoDB
mongoose.connect(config.database,options);
mongoose.connection.on('connected', ()=>{
console.log('connected to database '+config.database);
});
mongoose.connection.on('error',(err)=>{
console.log('Database error: '+err);
});
//routes
app.use('/api', require('./routes/api'));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// app.get('*',(req,res)=>{
// res.sendFile(path.join(__dirname,'public/index.html'))
// });
//start server
app.listen(port,function(){
console.log('API is running on port 3000');
});