-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (42 loc) · 1.41 KB
/
index.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
require('dotenv').config({path: __dirname + '/.env'});
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
const app = express();
mongoose.connect(process.env.DATABASE_ATLAS, {
useNewUrlParser: true,
useFindAndModify: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
autoCreate : true,
setDefaultsOnInsert: true
})
.then(() => {console.log("DB connected");
})
.catch((err)=> console.error("DB Connection errror", err));
// Routes
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/users');
// Applicationn Middelwares
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(cors()); // allows all origins
// if ((process.env.NODE_ENV = 'development')) {
// app.use(cors({ origin: `http://localhost:3000` }));
// }
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();
});
// Middlewares
app.use('/api',authRoutes);
app.use('/api',userRoutes);
const port = process.env.PORT || 8000;
app.listen(port, () => {
console.log(`API is running on port ${port} - http://localhost:${port} - ${process.env.NODE_ENV}`);
});