-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (50 loc) · 1.77 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
import express from 'express';
import morgan from 'morgan';
import chalk from 'chalk';
import mongoose from 'mongoose';
import { ApolloServer, PubSub } from 'apollo-server-express';
import { importSchema } from 'graphql-import';
import dotenv from 'dotenv';
import http from 'http';
// resolvers ..
import resolvers from './graphql/resolvers';
// Models
import User from './models/User';
import Message from './models/Message';
// load env files
dotenv.config();
const pubsub = new PubSub();
const server = new ApolloServer({
typeDefs: importSchema('./graphql/schema.graphql'),
resolvers,
context: {
User,
pubsub,
Message,
},
});
const app = express();
const morganMiddleware = morgan((tokens, req, res) => [
'\n',
chalk.hex('#ff4757').bold('Morgan --> '),
chalk.hex('#34ace0').bold(tokens.method(req, res)),
chalk.hex('#ffb142').bold(tokens.status(req, res)),
chalk.hex('#ff5252').bold(tokens.url(req, res)),
chalk.hex('#2ed573').bold(`${tokens['response-time'](req, res)} ms`),
chalk.hex('#f78fb3').bold(`@ ${tokens.date(req, res)}`),
chalk.yellow(tokens['remote-addr'](req, res)),
chalk.hex('#fffa65').bold(`from ${tokens.referrer(req, res)}`),
chalk.hex('#1e90ff')(tokens['user-agent'](req, res)),
].join(' '));
app.use(morganMiddleware);
// database connection
mongoose.set('useCreateIndex', true);
mongoose.connect(process.env.DB_URI, { useNewUrlParser: true })
.then(() => console.log(chalk.green.bold(' 🌈 MongoDB Connected..')))
.catch(e => console.log(chalk.red.bold(e)));
server.applyMiddleware({ app });
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
httpServer.listen({ port: 4001 }, () => {
(console.log(chalk.red.bold(` 🦄 Server ready at http://localhost:4001${server.graphqlPath}`)));
});