Getting proxy error when using "gulp serve" when API returns 204 with no content

I am using Gulp to develop an Angular application generated by Yeoman's gulp-angular generator. I have configured it to proxy requests to /api to another port, the port my API is listening on. That port is actually forwarded via an SSH tunnel to an external server.

Here is the config generated by Yeoman that I have edited for my own API:

gulp/server.js

'use strict';

var gulp = require('gulp');

var browserSync = require('browser-sync');
var httpProxy = require('http-proxy');

/* This configuration allow you to configure browser sync to proxy your backend */
var proxyTarget = 'http://localhost:3434/api'; // The location of your backend
var proxyApiPrefix = 'api'; // The element in the URL which differentiate between API request and static file request

var proxy = httpProxy.createProxyServer({
  target: proxyTarget
});

function proxyMiddleware(req, res, next) {
  if (req.url.indexOf(proxyApiPrefix) !== -1) {
    proxy.web(req, res);
  } else {
    next();
  }

// ...rest of config truncated

stdout

[BS] Watching files...

/Users/jason/dev/web/node_modules/http-proxy/lib/http-proxy/index.js:114
    throw err;
          ^
Error: Parse Error
    at Socket.socketOnData (http.js:1583:20)
    at TCP.onread (net.js:527:27)

I get the above error when my application attempts to hit a particular API url which sends back a response of 204, no content.

url structure: POST /api/resource/delete (API doesn't support actual DELETE http method so we POST to this endpoint)

Response: 204 No Content

The API is also in development and is being served via the built in PHP web server. What the server is telling us is that the client (aka Node in this case because it is the proxy) is hanging up before PHP can send the response.

I thought perhaps it was just choking on the fact that there was no content. So, we created a second endpoint that also returned 204 No Content and it seemed to work fine. But, to be fair, this issue appears to be intermittent - it works sometimes and sometimes it does not. It's very confusing.

As far as we can tell, it only happens on this delete URL, however. I am pretty new to Node and am having a very hard time figuring out what the issue is or where to look. Does anyone have any clues or has anyone seen this before?

It turns out that the developer of the API was sending me content along with his 204 when he shouldn't have been - some debug code left in. The HTTP parser that node-proxy uses was then reading that content from the buffer at the beginning of the subsequent request and then throwing an error because it wasn't seeing a properly formed HTTP request - since the first thing in the buffer was a PHP var_dump.

As it happens, my front end app did the delete call and then refreshes another object via the GET request. They happen so fast that it seemed like the DELETE call killed the gulp server, when it was actually the GET command afterwards.

The http-proxy module for node explicitly does not do error handling, leaving the onus on the end user. If you don't handle an error, it bubbles up into an uncaught exception and will cause the application to close, as I was seeing.

So, the fix was simply:

gulp/server.js

var proxy = httpProxy.createProxyServer({
  target: proxyTarget
}).on('error', function(e) {
  console.log(JSON.stringify(e, null, ' '))
});

The console will now log all proxy errors, but the process won't die and subsequent requests will continue to be served as expected.

For the error in question, the console output is:

{
 "bytesParsed": 191,
 "code": "HPE_INVALID_CONSTANT"
}

Additionally, we've fixed the API to honor its 204 and actually, you know, not send content.