Skip to content
Permalink
Browse files

Serve the React production build from the Express app

  • Loading branch information...
bhubr committed Aug 31, 2019
1 parent 8beb687 commit d7cba4cdb703bfd74f5f45da51a401952c3a43bc
Showing with 15 additions and 0 deletions.
  1. +15 −0 packages/back/src/app.js
@@ -1,10 +1,25 @@
// src/app.js
import express from 'express';
import bodyParser from 'body-parser';
import path from 'path';

// Check whether we are in production env
const isProd = process.env.NODE_ENV === 'production';

const app = express();
app.use(bodyParser.json());

app.get('/api/foo', (req, res) => res.json({ foo: 'bar' }));

if (isProd) {
// Compute the build path and index.html path
const buildPath = path.resolve(__dirname, '../../front/build');
const indexHtml = path.join(buildPath, 'index.html');

// Setup build path as a static assets path
app.use(express.static(buildPath));
// Serve index.html on unmatched routes
app.get('*', (req, res) => res.sendFile(indexHtml));
}

module.exports = app;

0 comments on commit d7cba4c

Please sign in to comment.
You can’t perform that action at this time.