-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
46 lines (40 loc) · 1.37 KB
/
app.js
File metadata and controls
46 lines (40 loc) · 1.37 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
const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const bp = require('body-parser');
const app = express();
const PORT = process.env.PORT || 4000;
const route = require('./routes/routes');
const url = `mongodb://localhost:27017/mern-stack`;
// MODELS
require("./models/user.model");
require("./models/partenaire.model");
// Mongoose config
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || url);
// using the router to the app
app.use(route);
app.use(bp.json());
app.use(bp.urlencoded({extended: false}));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:3000');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
require('./routes/user.routes')(app);
if(process.env.NODE_ENV === "production") {
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'))
});
}
// default and basic routes
route.get('/', (req,res) => {
return res.send("Home page");
});
app.listen(PORT, function(err){
if(err) return console.error(err);
console.log(`server running on port ${PORT}`);
});