」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 我在nodejs中的子網域代理伺服器

我在nodejs中的子網域代理伺服器

發佈於2024-08-06
瀏覽:462

My subdomain proxy server in nodejs

這裡是nodejs代理伺服器,可以用來監聽子網域路由。

例如,我運行的伺服器是localhost:5000,但我想在其中使用子網域,例如subdomain1.localhost:5000或其他不同的東西。

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');
});
版本聲明 本文轉載於:https://dev.to/akram6t/my-subdomain-proxy-server-in-nodejs-51kc?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3