"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > My subdomain proxy server in nodejs

My subdomain proxy server in nodejs

Published on 2024-08-06
Browse:313

My subdomain proxy server in nodejs

Here is the nodejs proxy server that can be use to listen subdomain routes.

for example I run a server is localhost:5000 but I want to use subdomain in this like subdomain1.localhost:5000 or something diferent.

const express = require('express');
const app = express();
const httpProxy = require('http-proxy');

const proxy = httpProxy.createProxy();

const BASE = "https://github.com";

app.use((req, res, next) => {

     const hostname = req.hostname;
     const domains = hostname.split('.');
     const subdomain = domains[0];
     const resolveTo = BASE   '/'   subdomain;
     return proxy.web(req, res, { target: resolveTo, changeOrigin: true });
});

app.listen(5000, () => console.log('Listening on port: 5000'));

app.get('/', (req, res) => {
     return res.send('Welcome to the homepage');
});
Release Statement This article is reproduced at: https://dev.to/akram6t/my-subdomain-proxy-server-in-nodejs-51kc?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3