1

Introduction

I have a university-managed server, that does not give students access to open ports for internet traffic, but students could still open ports (like 4040 for their NodeJS applications for internal access).

Each student's account has a public_html folder (similar to /var/www/), which serves all files in public_html on this URL (using an Apache server) statically (or rendered by PHP):

http://mi-linux.wlv.ac.uk/~studentid/

Problem

However, in my case, I want to expose an API for external testing using postman and adding it to a React application. The problem is, it uses NodeJS and express, and creates its own server on port 4040.

I could access the API by using the curl command from ssh internally like:

studentid@csl-student:~$ curl http://localhost:4040
{"message": "Hey! The backend is working. Explore routes from the code"}

Now, since students cannot allow open posts for HTTP traffic, one could simply not access my NodeJS API from outside the server (which I have to).

Things I looked up

I searched extensively on topics like how to statically serve a nodejs application, to how to forward ports from 8080 (https port) to 4040 (my NodeJS port), but most of them require sudo access, and some of them simply don't work.

Solution I propose

I think I still could access my public_html folder, that is statically served, and could render PHP. I know that I could create a index.html (or index.php) file that would fetch http://localhost:4040 internally, and simply forward the result, since the API port is open internally, and the index.html file could be served externally.

What the file could do

While loading the file using

http://mi-linux.wlv.ac.uk/~studentid/index.html

The file could load the response from localhost:4040 internally on the server itself (since the API is accessible internally), then send the result along with the status codes and headers.

However, my API has several routes, and I could not manage to hard code each route. There must be a more efficient way of doing this.

What I'm looking for

I would be really thankful if one could direct me to a package already made for the purpose of forwarding static files with responses pre-loaded from an internal API.

Or I could have a php script that could do all the forwarding that I need and make the API public.

PS: I know this should be asked on Server Fault, but since I think this could be done by using a script or something, I asked it here.

Just Mohit
  • 141
  • 1
  • 13
  • 1
    I'd say you need to ask the people responsible for those limitations. They should be able to name a solution for your requirement, _if it is legit ..._ – arkascha Jun 17 '21 at 17:33
  • @arkascha I would have loved to, but it is part of an assignment where I have to deploy a website, with less than 18 hours left. I need a temporary solution, or I could edit my assignment submission later if I get a good enough answer. Thought of adding a bounty to this question, but it says I need to wait 2 days, would be highly appreciated if you could upvote or do something similar to get it a bit more attention. Thanks... – Just Mohit Jun 17 '21 at 17:56

2 Answers2

1

A workaround for this would be to host your NodeJs code on a third part platform, say Heroku.

And create a PHP script that acts as a middleware proxy and does curl requests to your Heroku-Hosted-NodeJs API endpoint.

enter image description here

Hackinet
  • 3,252
  • 1
  • 10
  • 22
  • 1
    Yep, I know I could do this (host it somewhere else), that's what I included in the answer, but I don't know how to exactly do it, I'm looking for a script that could do this, while still preserving the status code... BTW, thanks for your time, would appreciate if you could edit the answer to include such a script. Thanks again. – Just Mohit Jun 20 '21 at 19:17
1

Are the Apache's plugins mod_rewrite and mod_proxy enabled on the server?

If so, maybe you could use ProxyReverse with mod_rewrite's P flag inside a .htaccess file as described in this post:

RewriteEngine On
RewriteRule ^/node/(.*)$ 
http://localhost:4040/$1 [L,P]
ProxyPassReverse /node http://localhost:4040

(I can't test it right now, so my answer is purely speculative, sorry.)

Another option, in case that the server conf cannot be modified, could be use mod_headers and Location, but I'm not sure if it will be transparent:

Header edit Location ^http://internal\.example\.com/(.*) http://example.com/$1
Pelayo
  • 114
  • 5
  • Also related: https://stackoverflow.com/questions/23027847/htaccess-specific-url-to-different-port – Pelayo Jun 22 '21 at 13:15