-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.js
More file actions
62 lines (51 loc) · 1.54 KB
/
Copy pathserver.js
File metadata and controls
62 lines (51 loc) · 1.54 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
/**
* Server Main entry point of the app
* @Author: Khalid Elshafie <abolkog>
* @Date: 2017-09-28T00:35:12+09:00
* @Email: Khalid@abolkog.com
*/
require('dotenv').config();
const express = require('express');
const path = require('path');
const cors = require('cors');
const mongoose = require('mongoose');
const bodayParser = require('body-parser');
const passport = require('passport');
//Intiailzie app with express
const app = express();
const UserRoutes = require('./routes/users');
const TaskRoutes = require('./routes/tasks');
//Database Connection
mongoose.Promise = global.Promise; // Fix Deprecation issue
mongoose.connect(process.env.MONGODB_URI, { useMongoClient: true });
mongoose.connection.on('connected', () => {
console.log('Connected to the database');
});
mongoose.connection.on('error', (err) => {
console.log(`Unable to connect to the database: ${err}`);
});
//Port to be used by the server
const _PORT = process.env.PORT;
//---------------- Middlewares ----------------//
//CROS MW
app.use(cors());
//Body Parser MW
app.use(bodayParser.json());
//Passport MW
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
//---------------- Middlewares ----------------//
//Static public folder
app.use(express.static(path.join(__dirname, 'public')));
//Index Rotuer
app.get('/', (req, res, next) => {
res.send('I am alive')
});
//Users Routes
app.use('/users', UserRoutes);
app.use('/tasks', TaskRoutes);
//Start the server
app.listen(_PORT, () => {
console.log('Server Started');
});