ホームページ >ウェブフロントエンド >jsチュートリアル >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 中国語 Web サイトの他の関連記事を参照してください。