【问题标题】:Node forward path request to another server节点转发路径请求到另一台服务器
【发布时间】:2015-08-28 07:57:16
【问题描述】:

目前有一个网站运行一个节点服务器来处理对example.com 的所有请求,我在一个单独的服务器(运行 apache)上创建了一个完全独立的 wordpress 博客,我希望在 example.com/blog@987654324 这样的路径上提供服务@ IP地址。 wordpress 服务器不共享任何代码,甚至不知道除了它自己之外的任何东西的存在

node/express 中以这种方式转发所有请求的最佳方式是什么?另外,新的 wordpress 服务器的 A/CNAME 记录应该指向什么?

【问题讨论】:

标签: node.js express routing routes request


【解决方案1】:

工作和测试

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
app.use('*', createProxyMiddleware({target: 'https://yourdomain.com', changeOrigin: true}));
app.listen(3000);

先安装以下

npm i -s express
npm i -s http-proxy-middleware

【讨论】:

  • @maruf 请检查这个答案
【解决方案2】:

这可行:这是您需要在节点应用程序中放入的内容:

var express = require('express');
var app = module.exports = express();

var proxy = require('http-proxy').createProxyServer({
    host: 'http://your_blog.com',
    // port: 80
});
app.use('/blog', function(req, res, next) {
    proxy.web(req, res, {
        target: 'http://your_blog.com'
    }, next);
});

app.listen(80, function(){
    console.log('Listening!');
});

然后在您的 wordpress 应用程序中,您需要将站点 URL 设置为 http://your_node_app.com/blog

注意:您可能已经有一个 express 应用,其中可能包含 body-parser 中间件,其中 causes errorsgenerally doesn't play well with POST requests in http-proxy

您会在这些线程中找到几个解决方案,即 1:只需将此代理放在 body-parser 之前,或 2:使用 connect-restreamer

【讨论】:

猜你喜欢
  • 2012-06-07
  • 2012-04-23
  • 2012-12-27
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-26
  • 1970-01-01
相关资源
最近更新 更多