Skip to content

Instantly share code, notes, and snippets.

@madox2
Last active April 14, 2019 14:01
Show Gist options
  • Save madox2/a055f6abdd51f4fccbf82dc3d1c29867 to your computer and use it in GitHub Desktop.
Save madox2/a055f6abdd51f4fccbf82dc3d1c29867 to your computer and use it in GitHub Desktop.
node js server that serves static folder and makes proxy to api server
var proxy = require('express-http-proxy')
var express = require('express')
var app = express()
// e.g. I want to serve app from local dir on localhost:8000/app
// and use API from remote server http://myserver/api
const localAppStaticDir = '../somedir/someapp/build'
const localAppPath = '/app'
const localApiPath = '/api'
const serverApiPath = '/api'
const serverUrl = 'http://myserver'
const port = 8000
console.log('starting proxy')
app.use(localApiPath, proxy(serverUrl, {
proxyReqOptDecorator: function(proxyReqOpts, originalReq) {
// here can be modified the request (headers, etc.)
// proxyReqOpts.rejectUnauthorized = false
return proxyReqOpts
},
proxyReqPathResolver: function (req) {
return serverApiPath + req.url
},
}))
app.use(localAppPath, express.static(localAppStaticDir))
app.listen(port, '0.0.0.0')
console.log('app listening at ' + port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment