forked from sarkistlt/graphql-auto-generating-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
41 lines (35 loc) · 1.36 KB
/
server.js
File metadata and controls
41 lines (35 loc) · 1.36 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
import graphQLHTTP from 'express-graphql';
import webpackDevMiddleware from 'webpack-dev-middleware';
import webpack from 'webpack';
import express from 'express';
import path from 'path';
import schema from './schema';
import CMSConfig from './cms_config';
import config from '../webpack.config';
const NODE_ENV = process.env.NODE_ENV || 'development';
/**
* you can just use "import graphqlCMS from '';"
* I use NODE_ENV only for development reason to be able use hot module reloading
*/
const folder = (NODE_ENV === 'development') ? 'src' : 'lib';
const graphqlCMS = require(`../${folder}/middleware`).default;
const app = express();
if (NODE_ENV === 'development') {
const compiler = webpack(config);
app.use(webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
}));
app.use(require("webpack-hot-middleware")(compiler));
}
else {
app.get('/app.js', (req, res) => res.sendFile(path.resolve(__dirname, './public/app.js')))
app.get('/', (req, res) => res.sendFile(path.resolve(__dirname, './public/index.html')))
}
// running graphqlCMS middleware
app.use('/graphql_cms_endpoint', graphqlCMS(CMSConfig));
// running graphQL API endpoint
app.use('/graphql', graphQLHTTP({ schema, graphiql: true, pretty: true }));
// running server on http://localhost:7700/
app.listen(7700, function () {
console.log('Example app listening on port 7700!\n');
});