Last active
April 14, 2019 14:01
-
-
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
This file contains hidden or 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
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