-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.mjs
57 lines (50 loc) · 1.43 KB
/
server.mjs
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
import dotenv from "dotenv";
dotenv.config({ path: "./config/.env" });
import express from "express";
import { graphqlHTTP } from "express-graphql";
import graphQlSchema from "./graphql/schema/index.mjs";
import graphQlResolvers from "./graphql/resolvers/index.mjs";
import resolve from "path";
import isAuth from "./middleware/is-auth.mjs";
import cors from "cors";
// import {
// autoComplete,
// getAutoCompleteData,
// } from "./autocomplete/autocomplete.mjs";
const app = express();
const port = process.env.PORT || 5000;
import connectDB from "./config/db.config.mjs";
connectDB();
app.use(express.json());
app.use(cors());
app.use((req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
if (req.method === "OPTIONS") {
return res.sendStatus(200);
}
next();
});
app.use(isAuth);
app.use(
"/graphql",
graphqlHTTP({
schema: graphQlSchema,
rootValue: graphQlResolvers,
graphiql: true,
})
);
//for production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build/"));
app.get("*", (req, res) => {
res.sendFile(resolve(__dirname, "client", "build", "index.html"));
});
}
//autoComplete
// app.get("/search", autoComplete);
// app.get("/get/:id", getAutoCompleteData);
app.listen(port, () => {
console.log(`server started at port ${port}`);
});