-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.js
80 lines (68 loc) · 2.08 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
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
78
79
80
/**
* Created by Soon on 2/22/16.
*/
import cookieParser from 'cookie-parser';
import express from 'express';
import graphQLHTTP from 'express-graphql';
import morgan from 'morgan';
import path from 'path';
import session from 'express-session';
import webpack from 'webpack';
import WebpackDevServer from 'webpack-dev-server';
import pkg from './package.json';
import { Schema } from './data/schema';
import webpackConfig from './webpack.dev.config';
import logger from './logger';
const APP_PORT = 3000;
const GRAPHQL_PORT = 8080;
const rootPath = path.join(__dirname);
const publicPath = path.join(rootPath, 'public');
const graphQLServer = express();
const MemoryStore = require('session-memory-store')(session);
graphQLServer.use(cookieParser());
graphQLServer.use(session({
store: new MemoryStore(),
secret: pkg.name,
resave: false,
saveUninitialized: true,
}));
graphQLServer.use('/graphql',
graphQLHTTP(request => ({
schema: Schema,
rootValue: request,
pretty: true,
graphiql: true,
context: request.session,
})),
);
graphQLServer.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}`,
));
// create a single instance of the compiler to allow caching
const devCompiler = webpack(webpackConfig);
// Start a webpack-dev-server
const app = new WebpackDevServer(devCompiler, {
publicPath: webpackConfig.output.publicPath,
contentBase: path.join(__dirname, 'build', 'public'),
hot: true,
stats: {
colors: true,
hash: false,
timings: true,
assets: false,
chunks: false,
chunkModules: false,
modules: false,
children: false,
},
historyApiFallback: true,
compress: true,
proxy: { '/graphql': `http://localhost:${GRAPHQL_PORT}` },
});
// Static files middleware
app.use(express.static(publicPath));
app.use(morgan('combined', { stream: logger.stream }));
app.listen(APP_PORT, 'localhost', () => {
console.log(`App Server is now running on http://localhost:${APP_PORT}`);
console.log('[webpack-dev-server]', `http://localhost:${APP_PORT}/webpack-dev-server/index.html`);
});