2

I'm trying to setup the Ghost blogging program (v0.4) in a subdirectory of another nodejs/express app. I was able to get it working following the steps outlined here: Node http-proxy and express

So, I setup the proxy to Ghost from my main app via Express like this (on my dev machine):'

var proxy = new httpProxy.createProxyServer();
server.get('/blog*', function (req, res, next) {
    proxy.web(req, res, {
        target: 'http://localhost:2368'
    });
});

This works to access the blog content. However, when I go to /blog/ghost/signin and try to login, I get a 404. As far as I can tell, the signin page doesn't go anywhere outside of the blog/ directory, so why would it fail?

If I view the blog directly (on port 2368), I can login just fine.

Community
  • 1
  • 1
Daren
  • 91
  • 1
  • 6

1 Answers1

2

You have defined a route for GET only, so you are only proxying GET requests, but login and signup uses a POST request. Usually a proxy rule in Apache or nginx will proxy all allowed methods for a given url, but since you defining the handler by method this doesn't happen.

The signup POST gets a 404 since it is handled by your first node application that doesn't know what to do.

In addition to POST you also need the DELETE method to be able click on the notification messages and to delete posts. I'm not sure if other methods are needed as well (OPTIONS), GET, POST and DELETE were the only ones I observed, you will see which method is failing if you take a look at the requests the page does e.g. with Firebug.

To fix this, add the same handler you have added with get for post,put and delete as well:

server.post('/blog*', function (req, res, next) {
    proxy.web(req, res, {
        target: 'http://localhost:2368'
    });
});
server.delete('/blog*', function (req, res, next) {
    proxy.web(req, res, {
        target: 'http://localhost:2368'
    });
});
server.put('/blog*', function (req, res, next) {
    proxy.web(req, res, {
        target: 'http://localhost:2368'
    });
});

This way, the admin interface works correct.

Alex Lehmann
  • 668
  • 1
  • 6
  • 11
  • 1
    Thanks, that got me further. However, now when I try to login, the Express server spins for awhile and then dies with the error: `Error: socket hang up at createHangUpError (http.js:1472:15) at Socket.socketCloseListener (http.js:1522:23) at Socket.EventEmitter.emit (events.js:95:17) at TCP.close (net.js:466:12)` I'm logging in the server.post call, so I know it's getting that far, but I don't see any logging happen from the Ghost side. – Daren Feb 02 '14 at 00:05
  • I solved it. I needed to put `server.use(express.urlencoded()); server.use(express.json());` after the routes and then the admin interface started working. Thanks for your help! – Daren Feb 02 '14 at 00:21
  • You also need to add a `PUT` route handler as well. Everything else worked, but the settings and user pages invoke `PUT` routes. – Kelly J Andrews May 21 '14 at 15:54
  • Are there any issues with using .all since you want to catch all HTTP verbs? `server.all('/blog*', ...` I am doing something similar. I see the admin interface but it is hanging on a socket error when it tries to authenticate a user. – user3233032 Apr 24 '15 at 18:38