Last active
October 1, 2016 12:37
-
-
Save nelreina/bab344082f46b5e0d48198855f17f0c5 to your computer and use it in GitHub Desktop.
A static server with proxy to /api. Good for serving react apps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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