11

Currently have a website running a node server that handles all requests for example.com and I created a completely separate wordpress blog on a separate server (running apache) that I would like served on a path like example.com/blog at 172.23.23.23 IP address. The wordpress server doesn't share any code or is even aware of the existence of anything besides itself

What is the best way in node/express to forward all requests in this manner? Also what should the A/CNAME records for the new wordpress server point to?

2
  • Checkout node.js serve a wordpress blog Commented Jun 12, 2015 at 11:07
  • Interesting I didn't know that existed, but not really what I'm looking for. Commented Jun 12, 2015 at 11:29

2 Answers 2

15

This works: here's what you need to put in your node app:

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

Then in your wordpress app you need to set the Site URL to http://your_node_app.com/blog

Note: You probably already have an express app with probably a body-parser middleware which causes errors and generally doesn't play well with POST requests in http-proxy

You'll find couple solutions in those threads, namely 1: just put this proxy before body-parser, or 2: use connect-restreamer

Sign up to request clarification or add additional context in comments.

2 Comments

using your, @laggingreflex, suggestion, I have this question: stackoverflow.com/questions/55736094/…
@laggingreflex, How to read the response status code and response body of proxy request?
12

Worked and Tested

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

Install the following first

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

2 Comments

@maruf kindly check this answer
I made a service/docker out of it github.com/nexys-system/fwd-request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.