Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-root deployment #1331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

moikaturns
Copy link

Tried to deploy the app to a path other than root and ran into some problems with links not working and resources not loading. Had reverse proxy at https://host:port/mysite/ forwarding to https://localhost:8080/ (BASE_URL set to former). Made some changes per this PR to make app work in both root and non-root deployment.

If there's a better way to achieve please advise.

Used the following to simulate a a non-root deployment pasted into app.js:

/**
 * Simulate reverse proxy.
 */
const http = require('http');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
http.createServer((req, res) => {
  // Get protocol (http or https)
  const protocol = req.connection.encrypted ? 'https' : 'http';
  // Get host (includes the host and port)
  const host = req.headers.host;
  // Get the full URL (protocol + host + path + query string)
  const fullUrl = `${protocol}://${host}${req.url}`;
  if (fullUrl.indexOf(`${process.env.BASE_URL}`)>=0) {
    req.url = req.url.replace(/^\/mysite/, '');
    proxy.web(req, res, { target: 'http://localhost:8080' }); // Change target as needed
  } else {
    console.log(`failed to load ${fullUrl}`);
    res.writeHead(404, { 'Content-Type': 'text/plain' });
    res.end('404 Not Found');
  }
}).listen(3000, () => console.log('Reverse proxy running on port 3000'));

Needs these deps:

npm install http --save-dev && npm install http-proxy --save-dev

@YasharF
Copy link
Collaborator

YasharF commented Feb 24, 2025

Considering that the primary users are hackathon participants. I am not sure if it would be a good idea to add the extra complexity. At the moment I am having a hard time seeing the use case in the context of a hackathon. Feel free to comment on the use case.

@moikaturns
Copy link
Author

moikaturns commented Feb 24, 2025

App works well on root as-is so if hackathon primary purpose no need for this PR. Maybe worth acknowledging this behaviour on README. Am keeping the branch for this change on my fork so if users have this need in future they can consider the commit from there. Thanks again.
Edit: in my case I'm deploying to the same domain multiple different apps so each needs their own unique path so they can be co-located on the same domain e.g.http://host/app1 http://host/app2 etc.
For hackathons is easy to allocate different ports I imagine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants