You should be able to develop the SPA locally though and consume the APIs remotely but you probably do not want to allow cross domain requests or even separate the application in two different domains.
A reverse proxy should help you big time. With a reverse proxy you can concentrate on just developing your SPA bits locally while hitting the existing API remotely and yet being able to remotely deploy the app when ready. All you need to do is detect where the SPA is running and route through the local proxy the API requests.
Node http-proxy can be used to create an https-to-https proxy as presented below:
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
/* | |
- https-to-https-proxy.js: Tested with Apache as target host | |
- Preconditions: Have key/cert generated as in: | |
openssl genrsa -out key.pem 1024 | |
openssl req -new -key key.pem -out cert.csr | |
openssl x509 -req -days 3650 -in cert.csr -signkey key.pem -out cert.pem | |
*/ | |
var proxyTargetHost = 'test.sample.com', | |
proxyTargetPort = 443, | |
proxyLocalHost = 'localhost', | |
proxyLocalPort = 8443, | |
sslKeyPath = 'key.pem', | |
sslCertPath = 'cert.pem', | |
https = require('https'), | |
httpProxy = require('http-proxy'), | |
fs = require('fs'); | |
var proxy = httpProxy.createServer({ | |
ssl: { | |
key: fs.readFileSync(sslKeyPath, 'utf8'), | |
cert: fs.readFileSync(sslCertPath, 'utf8') | |
}, | |
target: 'https://' + proxyTargetHost + ':' + proxyTargetPort, | |
secure: false, // Depends on your needs, could be false. | |
rejectUnauthorized: false, | |
hostRewrite: proxyLocalHost + ':' + proxyLocalPort | |
}).listen(proxyLocalPort); | |
proxy.on('proxyReq', function(proxyReq, req, res, options) { | |
proxyReq.setHeader('Host', proxyTargetHost); | |
}); | |
proxy.on('proxyRes', function (proxyRes, req, res) { | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With"); | |
res.setHeader("Access-Control-Allow-Credentials", "true"); | |
}); |
2 comments:
Really interesting
Un Saludo hermano
Josu, un abrazo chaval!
Post a Comment