-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
41 lines (37 loc) · 1.53 KB
/
index.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
/* eslint-disable no-undef */
const { ApolloServer } = require("apollo-server-express");
const { ApolloServerPluginLandingPageLocalDefault } = require("apollo-server-core");
const typeDefs = require("./Schema/typedef");
const resolvers = require("./Schema/resolvers");
const express = require("express");
const app = express();
const cors = require("cors");
const mongoose = require("mongoose");
if (process.env.NODE_ENV !== "production") {
require("dotenv").config({ path: "./.env" });
}
(async () => {
const dbURI = `mongodb+srv://${process.env.USERNAME}:${process.env.PASSWORD}@${process.env.DBNAME}.0hyd2qk.mongodb.net/?retryWrites=true&w=majority`;
await mongoose.connect(dbURI)
.then(() => console.log("Database connected"))
.catch((error) => console.log(error));
app.use(cors({ origin: ["http://localhost:8000","https://charitycrowd-ke.web.app"] }));
app.use((req, res, next) => {
// Website you wish to allow to connect
//http://localhost:8000
// https://fund-project.netlify.app
res.setHeader("Access-Control-Allow-Origin", "http://localhost:8000");
// Pass to next layer of middleware
next();
});
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [
ApolloServerPluginLandingPageLocalDefault({ embed: true })
]
});
await server.start();
server.applyMiddleware({ app, path: "/", cors: true });
app.listen(process.env.PORT, () => console.log("Server is now running on port 7000"));
})();