-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (61 loc) · 1.74 KB
/
server.js
File metadata and controls
77 lines (61 loc) · 1.74 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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require("cors");
const morgan = require('morgan');
const mongoose = require("mongoose");
const app = express();
app.use(morgan("dev"));
const grapqlHttp = require('express-graphql')
//define server running port
let port = 4000;
// import db
const dbConfig = require('./app/config/db.config')
// import graphql schema
const graphQlSchema = require('./app/graphql/schema/schema')
// import grapgql resolver
const graphqlResolvers = require('./app/graphql/resolvers/resolver')
// import routes
const mainRoutes = require('./app/route/main.routes');
// import middleware
const isAuth = require('./app/middlewares/is-auth.middleware')
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(isAuth)
app.use('/main', mainRoutes)
app.use('/grapgql', grapqlHttp({
schema: graphQlSchema,
rootValue: graphqlResolvers,
graphiql: true
}));
app.use((req, res, next) => {
const error = new Error("Not found");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
console.log(error);
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
// Connecting to the database
mongoose
.connect(dbConfig.url, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Successfully connected to the database now");
})
.catch((err) => {
console.log("Could not connect to the database. Exiting now...", err);
process.exit();
});
// open server
app.listen(port, () => {
console.log("Server is up and running on port numner " + port);
});