-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
48 lines (42 loc) · 1.33 KB
/
server.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
// added (express npm) => Minimalist web framework for node.
const express = require("express");
// added (morgan npm) => HTTP request logger middleware for node.js
const morgan = require("morgan");
// added required controller to the server
const routes = require("./controllers");
// creating an express application
const app = express();
// loads environment variables from a .env file into process.env.
require("dotenv").config();
// require models
var db = require("./models");
const { graphqlHTTP } = require("express-graphql");
const { schema, root } = require("./graphqlSchema/schema");
const PORT = process.env.PORT || 3001;
const appOrigin = process.env.APP_ORIGIN;
app.use(morgan("dev"));
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
// set the middleware for graphql - express (to handle the request)
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
})
);
// Add routes, both API and view
app.use(routes);
// Start the API server
// Sequelize connection
db.sequelize.sync({ force: false }).then(() => {
app.listen(PORT, () => {
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`);
});
});