-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (54 loc) · 1.9 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require('body-parser')
const passport = require('passport')
const path = require('path')
const profile = require('./routes/api/profile')
const posts = require('./routes/api/posts')
const users = require('./routes/api/users')
const dbUrl = require("./config/keys").MongoURL;
const constants = require("./config/constants");
const app = express();
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(passport.initialize())
app.use(function (req, res, next) {
// Website to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// // Request methods to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// // Request headers to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type,Authorization');
// res.setHeader('Access-Control-Request-Headers', 'X-Requested-With,content-type');
// // Include cookies in the requests sent
// // to the API
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
require('./config/passport')(passport)
//Use Routes
app.get("/", (req, res) => {
res.json({ "message": "Welcome" })
})
app.use("/api/posts", posts)
app.use("/api/profile", profile)
app.use("/api/users", users)
// if(process.env.NODE_ENV === 'production') {
// app.use(express.static("client/server/static/wV7s_Fsy4K0dtAcrDt5xs"))
// app.get('*', (req, res) => {
// res.sendFile(path.resolve(__dirname, 'client', 'out', 'index.html'))
// })
// }
const port = process.env.PORT || 5000;
mongoose
.connect(dbUrl, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() =>
console.log("Mongo Connected")
)
.catch(err => {
console.log(err)
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
})