1- /* eslint-disable */
1+ /* eslint-disable no-console */
22import express from 'express' ;
33import path from 'path' ;
44import morgan from 'morgan' ;
55import React from 'react' ;
6+ import env from 'node-env-file' ;
67import { renderToString , renderToStaticMarkup } from 'react-dom/server' ;
78import { match , RouterContext } from 'react-router' ;
89import { ApolloProvider } from 'react-apollo' ;
910import { getDataFromTree } from 'react-apollo/server' ;
1011import { createNetworkInterface } from 'apollo-client' ;
11- import store from '../app/src/store.js ' ;
12- import { routes } from '../app/src/routes.js ' ;
13- import { BASE_URL } from '../app/src/config ' ;
12+ import styleSheet from 'styled-components/lib/models/StyleSheet ' ;
13+ import store from '../app/src/store ' ;
14+ import { routes } from '../app/src/routes ' ;
1415import Html from './utils/Html' ;
1516import createApolloClient from './utils/create-apollo-client' ;
1617import manifest from './public/manifest.json' ;
17- import styleSheet from 'styled-components/lib/models/StyleSheet' ;
1818
19- const app = express ( ) ;
20- const isDeveloping = process . env . NODE_ENV !== 'production' ;
19+ env ( path . join ( __dirname , '..' , 'env' ) ) ;
2120
22- // Need to set this to your api url
23- const IP = process . env . IP || 'localhost' ;
24- const PORT = process . env . PORT || 1337 ;
25- const apiUrl = `${ BASE_URL } graphql` ;
21+ const app = express ( ) ;
22+ const serverUrl = process . env . BASE_URL || 'http://localhost:1337' ;
23+ const apiUrl = process . env . API_URL || 'http://localhost:3000' ;
24+ const PORT = serverUrl . match ( / \d + / g) [ 0 ] ;
25+ const IP = serverUrl . match ( / \w + / g) [ 1 ] ;
26+ const graphqlUrl = `${ apiUrl } graphql` ;
27+ const debug = process . env . DEBUG || false ;
2628
27- app . use ( morgan ( 'combined' ) ) ;
28- app . use ( express . static ( __dirname + '/public' ) ) ;
29+ if ( debug ) { app . use ( morgan ( 'combined' ) ) ; }
30+ app . use ( express . static ( path . join ( __dirname , '/public' ) ) ) ;
2931
3032app . use ( ( req , res ) => {
3133 match ( { routes, location : req . url } ,
@@ -40,7 +42,7 @@ app.use((req, res) => {
4042 const client = createApolloClient ( {
4143 ssrMode : true ,
4244 networkInterface : createNetworkInterface ( {
43- uri : apiUrl ,
45+ uri : graphqlUrl ,
4446 credentials : 'same-origin' ,
4547 headers : req . headers ,
4648 } ) ,
@@ -56,19 +58,19 @@ app.use((req, res) => {
5658 const html = (
5759 < Html
5860 content = { content }
59- scriptHash = { manifest [ " /main.js" ] }
60- vendorHash = { manifest [ " /vendor.js" ] }
61- cssHash = { manifest [ " /main.css" ] }
61+ scriptHash = { manifest [ ' /main.js' ] }
62+ vendorHash = { manifest [ ' /vendor.js' ] }
63+ cssHash = { manifest [ ' /main.css' ] }
6264 styles = { styles }
6365 state = { ctx . store . getState ( ) }
6466 />
6567 ) ;
6668 res . status ( 200 ) . send ( `<!doctype html>\n${ renderToStaticMarkup ( html ) } ` ) ;
67- } ) . catch ( e => console . error ( 'RENDERING ERROR:' , e ) ) ; // eslint-disable-line no-console
69+ } ) . catch ( e => console . error ( 'RENDERING ERROR:' , e ) ) ;
6870 } else {
6971 res . status ( 404 ) . send ( 'Not found' ) ;
7072 }
71- } )
73+ } ) ;
7274} ) ;
7375
7476app . listen ( PORT , IP , ( err ) => {
@@ -77,4 +79,3 @@ app.listen(PORT, IP, (err) => {
7779 }
7880 return console . info ( `==> 😎 Listening on port ${ PORT } . Open http://${ IP } :${ PORT } in your browser.` ) ;
7981} ) ;
80- /* eslint-enable */
0 commit comments