-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
91 lines (68 loc) · 2.75 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const express = require('express');
//const methodOverride = require('method-override');
//var cors = require('cors')
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const userRoutes = require('./api/routes/user');
const groupRoute = require('./api/routes/group');
const categoryRoute = require('./api/routes/category');
const productRoute = require('./api/routes/product');
const contactRoute = require('./api/routes/contact');
const orderRoute = require('./api/routes/order');
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/meeks', {useNewUrlParser: true});
app.use(morgan('dev'));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
// app.use((req, res, next) => {
// res.header('Access-Control-Allow-Origin', 'https://meeks-onlinestore-client.herokuapp.com');
// res.header('Access-Control-Allow-Headers', 'XMLHttpRequest, Origin, Content-Type,Content-Length, Authorization, Accept,X-Requested-With');
// if(req.method === 'OPTIONS'){
// res.header('Access-Control-Allow-Methods', 'DELETE, PUT, POST, PATCH, GET');
// return res.status(200).json({});
// }
// });
app.all("*", function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header("Access-Control-Allow-Headers", "Origin,Content-Type,Content-Length, Authorization, Accept,X-Requested-With,XMLHttpRequest");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS, PATCH");
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
});
//app.options('*', cors());
//app.use(cors());
//app.use(methodOverride('_method'));
//app.use(methodOverride('X-HTTP-Method-Override'))
// Routes which should handle requests
app.use('/user', userRoutes);
app.use('/groups', groupRoute);
app.use('/categories', categoryRoute);
app.use('/products', productRoute);
app.use('/contacts', contactRoute);
app.use('/orders', orderRoute);
app.use(function (req, res, next) {
const error = new Error('Not Found');
error.status = 404;
next(error);
});
// app.use(function (err, req, res, next) {
// if(err.name === 'ValidationError'){
// const valErrors = [];
// Object.keys(err.errors).forEach(key => valErrors.push(err.errors[key].message));
// res.status(422).json(valErrors)
// }
// });
// app.use(function (err, req, res, next) {
// if(err.name === 'CastError'){
// const valErrors = [];
// Object.keys(err.errors).forEach(key => valErrors.push(err.errors[key].message));
// res.status(422).json({
// error: 'required'
// })
// }
// });
module.exports = app;