I develop an webapp, against an api. As the api is not running on my local system, I need to proxy the request so I dont run in cross domain issues. Is there an easy way to do this so my index.html will send from local and all other GET, POST, PUT, DELETE request go to xyz.net/apiEndPoint.
Edit:
this is my first solution but didnt work
var express = require('express'),
app = express.createServer(),
httpProxy = require('http-proxy');
app.use(express.bodyParser());
app.listen(process.env.PORT || 1235);
var proxy = new httpProxy.RoutingProxy();
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.get('/js/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.get('/css/*', function(req, res) {
res.sendfile(__dirname + req.url);
});
app.all('/*', function(req, res) {
proxy.proxyRequest(req, res, {
host: 'http://apiUrl',
port: 80
});
});
It will serve the index, js, css files but dont route the rest to the external api. This is the error message I've got:
An error has occurred: {"stack":"Error: ENOTFOUND, Domain name not found\n at IOWatcher.callback (dns.js:74:15)","message":"ENOTFOUND, Domain name not found","errno":4,"code":"ENOTFOUND"}
In a nutshell, a proxy is an intermediary application which sits between two (or more) services and processes/modifies the requests and responses in both directions.
Central registry: an npm proxy acts as a central registry for all your required package versions.
A reverse proxy receives every request from the client, acting as an intermediary server, then forwards these requests to the actual server(s) that should handle the request. The response from the server is then sent through the proxy to be delivered to the client as if it came from the web server itself.
Take a look at the readme for http-proxy
. It has an example of how to call proxyRequest
:
proxy.proxyRequest(req, res, {
host: 'localhost',
port: 9000
});
Based on the error message, it sounds like you're passing a bogus domain name into the proxyRequest
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With