nelreina
9/11/2016 - 11:25 AM

A static server with proxy to /api. Good for serving react apps

A static server with proxy to /api. Good for serving react apps


const express = require('express');
const path = require('path');
const request = require('request');

const app = express();

const port = process.env.PORT || 4000;
const baseUrl = process.env.BASE_URL || '/registration';
const apiPort = process.env. API_PORT || 9000;
const API = `http://0.0.0.0:${apiPort}`;

app.use(baseUrl ,express.static(__dirname));

app.get('/api/*', (req, res) => req.pipe(request.get(`${API}${req.path}`)).pipe(res) );
app.post('/api/*', (req, res) => req.pipe(request.post(`${API}${req.path}`)).pipe(res) );

app.get('*', (req, res) => res.sendFile(path.join( __dirname, 'index.html')));

app.listen(port);

console.log(`App is running on port ${port} connected to API  ${API}`);

console.log(`App is running on port ${port} with proxy to ${proxyPort}`);