nodejs reverse proxy instead of nginx as reverse proxy - node.js

I'm developing a web application using nodejs and I need a reverse proxy for this application.
In many places it is noticed that nginx is used as a reverse proxy.
My questions are
1. "Is there any ready made nodejs based reverse proxy?"
2. "Is it a good idea to implement a nodejs based reverser proxy?"
3. "It is advised to use nginx?"
4. Why is nginx is considered in first place for reverse proxy?
--Ganesh

use following command to install Express.js and http-proxy.
npm install --save express http-proxy
In order to run the reverse proxy server we need some resource server from which Proxy will fetch data.So to do that, develop three Express server running on Port 8000,8001,8002 respectively.
Server.js
var express = require("express");
var app = express();
app.get('/app1',function(req,res) {
res.send("Hello world From Server 1");
});
app.listen(8000);
write the same code for other servers too and change the text.
Example of proxy server code in express.js with multiple targets.
app.js (file)
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
var serverOne = 'http://localhost:8000',
ServerTwo = 'http://localhost:8001',
ServerThree = 'http://localhost:8002';
app.all("/app1/*", function(req, res) {
console.log('redirecting to Server1');
apiProxy.web(req, res, {target: serverOne});
});
app.all("/app2/*", function(req, res) {
console.log('redirecting to Server2');
apiProxy.web(req, res, {target: ServerTwo});
});
app.all("/app2/*", function(req, res) {
console.log('redirecting to Server3');
apiProxy.web(req, res, {target: ServerThree});
});
app.listen(8000);
You can add as many targets as you want and it will create a proxy for that.Check whether its working or not by first run all the servers and hit request to /app1 and /app2 etc.

I found the DProx node plugin an absolute must. Allows for most of the configuration that one would need from a Reverse proxy server, while making it simple JSON config.

Related

Node JS server proxy using additional paths not working

I have a simple node js server like this:
var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
var serverOne = 'http://<address>:<port>/sap/opu/odata/sap/Z_ATTENDANCE_SRV/';
app.use(express.static('webapp'));
app.use(express.static('./'));
app.all("/attendance/*", function(req, res) {
console.log('redirecting to Server1: ' + serverOne);
apiProxy.web(req, res, {target: serverOne});
});
app.listen(3000);
So address localhost:3000/attendance should redirect me to http://<address>:<port>/sap/opu/odata/sap/Z_ATTENDANCE_SRV/ but it is not, I am getting 404.
I was able to make it work when I set the proxy path as "/*" instead of "/attendance/*", but when I wanted to access entity set "AttendanceSet" via localhost:3000/AttendanceSet it also gave me 404. Do I need to create proxy for all my paths? Shouldn't the /* do that?
When I check initialisation of oDataModel in SAPUI5 app I can see such a request (In this case I have set "/*" for the proxy):
Request URL: http://localhost:3000/$metadata?sap-language=EN
Request Method: GET
Status Code: 200 OK (from disk cache)
Remote Address: [::1]:3000
Referrer Policy: no-referrer-when-downgrade
By this logic, I should be able to access entity set AttendanceSet, but I guess I am missing something.
Thanks.
For urls like localhost:3000/AttendanceSet or localhost:3000/Attendance you have to remove the / you have in you endpoint like this.
app.all("/attendance*", function(req, res) {
console.log('redirecting to Server1: ' + serverOne);
apiProxy.web(req, res, {target: serverOne});
});
Looks like problem was with cache. I deleted browser cache and also, when running node js server I used addition -c-1 to do no caching.

Webpack proxy messing up my routing?

So I'm using webpack for a project on 8080 with a backend on 3000. The proxy seems to work fine, as I can send requests to the backend and access it without issue. However. I need to include this middleware that allows me to have a user load the page, and if they've logged in within a certain amount of time, the initial request they send to the server logs them in automatically.
router.use(function (req, res, next) {
//check token for routes beneath vvvv
})
router.post('/preauth', function (req, res) {
//return user account info if req.token is valid
})
When I try to get to prauth, or even any route before that from the page loaded on 8080 I only touch the middleware and nothing else.
When I do npm run build then try it again from the identical page on 3000, it works as expected.
No, CORS is not enabled and the proxy does not rewrite any url.
Does anyone know if something in my Webpack config might be causing this?
You need install Cors in nodejs:npm install cors, you can try the following below or you see: Nodejs + Vuejs
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('This is a CORS-enabled web server listening on port 80')
})

How to Proxy using Node using express-http-proxy to strip final rest

On my local JavaScript, I want to make a rest call to
/rest/speakers
and have that proxied to
http://localhost:2011/rest/speakers
I have code as follows that does not quite work as I want:
var proxy = require('express-http-proxy');
var app = require('express')();
app.use('/rest', proxy('localhost:2011/'));
app.listen(8081, function () {
console.log('Listening on port 8081');
});
To make the proxy work, I actually need to call
/rest/rest/speakers
I kind of get it. It seems that I'm proxying my /rest to the root of localhost:2011 and then I need to dive into the /rest of that server. Adding /rest to the end of the proxy(..) is ignored.
Try using proxyReqPathResolver to modify your URL as needed.
var url = require('url'),
proxy = require('express-http-proxy');
// ... other app setup here
app.use('/rest', proxy('localhost:2011', {
proxyReqPathResolver: function(req, res) {
return '/rest/rest' + url.parse(req.url).path;
}
}));

Use Proxy With Express middelware in NodeJS

I have in my private network different services, these services are not accessible from the out side (externe).
I'd like that my services be accessible to some users, and this after an authentication process (for this part I use express).
Once the user is authentificared it will be proxiate to the right service, I tried for this the http-proxy module.
Problem:
I failed to use correctly http-proxy with express module, and resolve this enigma as wished.
Code:
I began by doing this
// Create app with Express
var express = require('express');
var app = express();
// Create a proxy server with http-proxy
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer();
// Create target params (in the local network)
var serverOne = {target:'ws://172.17.0.3:80',ws:true};
// The use of proxy to expose the service
app.all("/app/", function(req, res) {
console.log('redirecting to Server1');
proxy.web(req, res, serverOne);
})
// The login part
.get('/login', function(req, res) {
res.render('login.ejs');
console.log('Cherche Login');
})
app.listen(8080);
Result:
Can someone help me please to fix this?
Try changing var serverOne = {target:'ws://172.17.0.3:80',ws:true};
to var serverOne = {target:'ws://172.1.0.3:80',ws:true};
Also check that from the server 104.155.15.204, you are able to access the 172.0.1.x network
You can have a look at http://expressjs.com/fr/api.html for implementation of proxies on express.
Regards,
Marc

Limit Node express to specific hostnames?

What's the best way to reject requests to my web server (running via Node express) that are coming in to an unrecognized hostname? I.e. I only want to respond to requests that are intended for my domain, not for requests that are just aimed at my IP address.
The easiest way would probably be to use the connect vhost middleware.
Where you would normally do this:
var app = express();
app.get('/', function(req, res){
res.send('HI');
});
app.listen(80);
You would do this:
var vhostApp = express();
vhostApp.get('/', function(req, res){
res.send('HI');
});
var app = express();
app.use(express.vhost('example.com', vhostApp));
app.listen(80);

Resources