Ngrok > Nginx > localhost ports - node.js

I have two node servers running on ports 3000 and 4000. If I had NGINX running and pointed ngrok at it, is there a way I can redirect requests from ngrok subfolders to each node server? eg abc123.nrgok.io/a goes to port 3000, abc123.ngrok.io/b goes to port 4000. There are several routes for each node server and some static html files too.

In the end I scrapped NGINX and instead ran a third node server which just had http-proxy-middleware running. That seemed to work much better but I did need to change the links in both apps to point at the sub folder and then rewrite the head in the new Node server (eg all the links went from style.css to /serverOne/style.css and then the proxy middleware strips off the /serverOne bit). Code below for the third Node server.
const http = require('http'),
express = require('express'),
{ createProxyMiddleware } = require('http-proxy-middleware')
const app = express()
app.use('/serverOne', createProxyMiddleware({target:'http://localhost:4000', changeOrigin: true, pathRewrite: {'^/serverOne' : '/'}}))
app.use('/serverTwo', createProxyMiddleware({target:'http://localhost:3000', changeOrigin: true, pathRewrite: {'^/serverTwo' : '/'}))
app.listen(2000);

Related

Setting up react and nodejs servers on same machine

I am setting up a reactjs application on port 3000 as well as a nodejs API server on port 3500 on the same box on the internet. Assume the box has a domain name example.com, and I am using nginx in reverse proxy to receive the end users over https, and internally direct it to the port 3000 of the reactjs server.
On the react code, while calling axios API for a get command, which of the following should I be using:
localhost:3500
http://localhost:3500
https://localhost:3500
example.com:3500
http://example.com:3500
https://example.com:3500
Few tests I did:
Access from my browser the reactjs application successfully as example.com (nginx does the mapping to port 3000)
Using https://reqbin.com/ I was able to access the nodejs server API and get the correct result using: http://example.com:3500/users
Using https instead of http causes an error: SSL connection error Error code: 10035
If end user is supposed to connect over https to the react server, then the react server as well as the nodejs server should be running in https mode, or the browser will block the request and it will never reach the server.
Here is how to:
Run the react server in https mode:
Change nginx reverse proxy configuration to be:
proxy_pass https://localhost:3000;
Changed the URL for the nodejs server that axios is calling from http://localhost:3500 to https://example.com:3500
After npm run build, and upload the build directory to the server, run the following commands:
su
serve -s build --listen 3000 --ssl-cert "/etc/letsencrypt/live/example.com/fullchain.pem" --ssl-key "/etc/letsencrypt/live/example.com/privkey.pem"
Run the nodejs server in https mode:
Change the code of server.js with the following:
const https = require('https');
const fs = require('fs');
const options = {
key: fs.readFileSync('/etc/letsencrypt/live/example.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/example.com/fullchain.pem')
};
https.createServer(options, app).listen(PORT, ()=>{
console.log(`Server running https on port ${PORT}`)
});
Run the following commands:
su
node server

Node.JS not serving port 80 on IIS

I stopped IIS on Windows Server 2019 completely, and Im serving my NodeJS app on port 80 as follows:
const express= require('express');
const app=express();
...
...
...
const hostname = '0.0.0.0';
const port = 80;
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
But Node is not serving remotely. It works with localhost:80
Ive set hostname = '0.0.0.0' based on an answer I've found here at StackOverflow. The server's public IP doesn't work either.
When turning IIS on and setting up the site, I can see an index.html file just fine, so port 80 works.
Is it a server issue, or a node.js issue?
Im starting node with
node index.js
Thanks.
For those facing this issue, I fixed it up as Tadman recommended on the above comments: configuring IIS for forwarding (by installing the Application Request Routing and the URL rewrite extensions to IIS).
Node.js also worked when creating a new rule at Windows firewall for enabling port 80. The default rule at the firewall for port 80 will work only with IIS, not with Node.js.
As for the extensions mentioned above, here is a good instructive about how to install and setup:
https://dev.to/petereysermans/hosting-a-node-js-application-on-windows-with-iis-as-reverse-proxy-397b

How can I run node js and apache with single ssl certificate with different port?

I am using nodejs on port number 3000 and my site is ssl-certified. After running node server.js I got below error:
GET https://--.---.-.---:3000/socket.io/?EIO=3&transport=polling&t=1513323461271-0 net::ERR_CONNECTION_REFUSED.
So, how can I run node js and apache with single ssl certificate with different port? And my site is hosted on Amazon Server.
Hope you already configured SSL in Apache
Node(Express)
const express = require('express');
const app = express();
var cors = require('cors')
app.use(cors());
var server = require('http').createServer(app);
Open httpd.conf in server and add ProxyPass line as u want
ProxyPass /api/ http://localhost:3000/
DONT FOGOT TO RESTART THE HTTP SERVICE
http://www.example.com/mypage apache http: OK!
https://www.example.com/mypage apache https: OK!
http://www.example.com/api/ node http: OK!
https://www.example.com/api/ node https: IT WORKKS !!!

Node.js Proxy to a site in Heroku

I'm trying to create a proxy in node.js that will redirect my request to miniclips.com
var http = require('http'),
httpProxy = require('http-proxy');
Create your proxy server and set the target in the options.
httpProxy.createProxyServer({target:'54.192.142.49'}).listen((process.env.PORT || 5000));
When I run it using localhost if I set the .listen(9000) to some port number it works fine. I go to my proxy settings and choose web proxy and select localhost:9000. However when I deployed this on Heroku I figured I had to change .listen to .listen((process.env.PORT || 5000)); I then set my proxy settings to the name-of-my-app.heroku.com and the port number to 80 but all I get is a Heroky no such app error.
https://support.dnsimple.com/articles/heroku-error-nosuchapp/

Running a forward proxy server in nodejitsu using node js

I am new to proxy server. What I want to do is: I want to write some node.js code, and then upload to my nodejitsu account to run as a proxy server. Then I would like to use my nodejitsu proxy server on my computer, by configuring the http proxy as "abc.jit.su" (my jitsu URL), and the port as "80" in Chrome, Firefox or IE. That's to say, I want my nodejitsu proxy server to have the same function as the proxies listed here: http://www.freeproxylists.net/. Any ideas?
You can write a simple proxy using the request module, like this:
var http = require('http'),
request = require('request');
// For nodejitsu, this will be port 80 externally
var port = process.env.PORT || 8000;
http.createServer(function(req,res) {
req.pipe(request(req.url)).pipe(res)
}).listen(port);
However, this will only work with http, not https.
Nodejitsu also produces a proxy module, you may get some ideas on what to do next by looking at that.

Resources