-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
92 lines (76 loc) · 2.49 KB
/
Copy pathserver.js
File metadata and controls
92 lines (76 loc) · 2.49 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
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const createError = require("http-errors");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const compression = require("compression");
const cors = require("cors");
const { createHandler } = require("graphql-http/lib/use/express");
const { schema } = require("./src/api/graphQL/schema");
const { root } = require("./src/api/graphQL/route/root");
const passport = require("passport");
require("./src/repository/db");
const ENV = process.env.NODE_ENV;
const app = express();
const port = process.env.PORT;
app.disable("x-powered-by"); //turn off header
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "hbs");
app.use(compression());
app.use(logger("dev"));
app.use(express.json({ extended: true }));
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
app.use(cors());
app.options("*", cors());
app.use(passport.initialize());
require("./src/api/auth/passportAuth")(passport);
//Enable CORS from client side
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "PUT,GET,DELETE,POST,OPTIONS");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With,Content-Type, Accept, Authorization, Apptoken," +
" Access-Control-Allow-Credential"
);
res.header("Access-Control-Allow-Credentials", "true");
res.language = req.query.language || "en";
res.langKey = req.query.langKey || false;
global._language = res.language;
next();
});
// // Create and use the GraphQL handler.
// app.all(
// "/graphql",
// createHandler({
// schema: schema,
// rootValue: root,
// })
// );
require("./src/api/restful")(app);
// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});
// Catch unauthorised errors
app.use(function (err, req, res, next) {
if (err.name === "UnauthorizedError") {
res.status(401);
res.json({ message: err.name + ": " + err.message });
}
});
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
module.exports = app;