首页  >  文章  >  web前端  >  我在nodejs中的子域代理服务器

我在nodejs中的子域代理服务器

王林
王林原创
2024-07-16 12:31:19673浏览

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');
});

以上是我在nodejs中的子域代理服务器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn