-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
132 lines (108 loc) · 3.16 KB
/
Copy pathapp.js
File metadata and controls
132 lines (108 loc) · 3.16 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const express = require("express");
//const fileUpload = require("express-fileupload");
const path = require("path");
const logger = require("morgan");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const routes = require("./server/routes/index");
const blogRoute = require("./routes/blog");
const useraccountRoute = require("./routes/useraccount");
const app = express();
const cors = require("cors");
const port = process.env.PORT || 5000;
const morgan = require("morgan");
app.use(morgan("combined"));
app.use(bodyParser.json({ limit: "50mb" }));
app.use(
bodyParser.urlencoded({
limit: "50mb",
extended: true,
parameterLimit: 1000000
})
);
app.use(cookieParser());
// app.use(express.static(path.join(__dirname, "client")));
//app.use(express.static(path.join(__dirname, "public"))); // configure express to use public folder
//app.use(fileUpload()); // configure fileupload
//app.use("/", routes);
// app.use(
// cors({
// origin: "*",
// exposedHeaders: [
// "Content-Range",
// "X-Content-Range",
// "Content-Disposition",
// "Content-Error"
// ],
// credentials: true
// })
// );
// app.use((req, res, next) => {
// res.header("Access-Control-Allow-Origin", "*");
// res.header(
// "Access-Control-Allow-Headers",
// "Origin, X-Requested-With, Content-Type,Accept, Authorization"
// );
// if (req.method === "OPTIONS") {
// res.header(
// "Access-Control-Allow-Methods",
// "PUT, POST, ,PATCH, DELETE, GET"
// );
// return res.status(200).json({});
// }
// next();
// });
app.use(function(req, res, next) {
/*var err = new Error('Not Found');
err.status = 404;
next(err);*/
// Website you wish to allow to connect
res.setHeader("Access-Control-Allow-Origin", "*");
// Request methods you wish to allow
res.setHeader(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE"
);
// Request headers you wish to allow
res.setHeader(
"Access-Control-Allow-Headers",
"Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers,X-Access-Token,XKey,Authorization"
);
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// Pass to next layer of middleware
next();
});
app.use("/api/v2/blog", blogRoute);
app.use("/api/v2/useraccount", useraccountRoute);
// app.use('/users', users);
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get("env") === "development") {
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.json({
message: err.message,
error: {}
});
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});
module.exports = app;