Skip to content

Instantly share code, notes, and snippets.

@nelreina
Last active October 1, 2016 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nelreina/bab344082f46b5e0d48198855f17f0c5 to your computer and use it in GitHub Desktop.
Save nelreina/bab344082f46b5e0d48198855f17f0c5 to your computer and use it in GitHub Desktop.
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}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment