TypeError: Cannot read property 'emit' of undefined in /node_modules/koa/lib/context.js:117:13
I have an EC2 server running Ubuntu 14.04 and I'm having problems with the API. We used Capistrano to deploy, and it has Nginx installed.

The API is running on port 3333 of the IP of the server. It works, because when accessing via browser, it returns a small JSON specifying `success` as `true`, to indicate the API is indeed being accessed. The problem is, when actually performing an operation from the client, the following error occurs:

    /var/apps/hp-order/releases/20180521074510/workspace/webroot/node_modules/koa/lib/context.js:117
        this.app.emit('error', err, this);

    TypeError: Cannot read property 'emit' of undefined
        at Request.onerror (/var/apps/hp-order/releases/20180521074510/workspace/webroot/node_modules/koa/lib/context.js:117:13)
        at emitOne (events.js:101:20)
        at Request.emit (events.js:188:7)
        at Request.onRequestError (/var/apps/hp-order/releases/20180521074510/workspace/webroot/node_modules/request/request.js:877:8)
        at emitOne (events.js:96:13)
        at ClientRequest.emit (events.js:188:7)
        at Socket.socketOnData (_http_client.js:372:9)
        at emitOne (events.js:96:13)
        at Socket.emit (events.js:188:7)
        at readableAddChunk (_stream_readable.js:176:18)
        at Socket.Readable.push (_stream_readable.js:134:10)
        at TCP.onread (net.js:559:20)

I commented out this line of code where the error shows and the next error message is 

    /var/apps/hp-order/releases/20180521074510/workspace/webroot/node_modules/koa/lib/context.js:129
        if (typeof res.getHeaderNames === 'function') {
                      ^
    
    TypeError: Cannot read property 'getHeaderNames' of undefined
        at Request.onerror (/var/apps/hp-order/releases/20180521074510/workspace/webroot/node_modules/koa/lib/context.js:129:19)
        at emitOne (events.js:101:20)
        at Request.emit (events.js:188:7)
        at Request.onRequestError (/var/apps/hp-order/releases/20180521074510/workspace/webroot/node_modules/request/request.js:877:8)
        at emitOne (events.js:96:13)
        at ClientRequest.emit (events.js:188:7)
        at Socket.socketOnData (_http_client.js:372:9)
        at emitOne (events.js:96:13)
        at Socket.emit (events.js:188:7)
        at readableAddChunk (_stream_readable.js:176:18)
        at Socket.Readable.push (_stream_readable.js:134:10)
        at TCP.onread (net.js:559:20)

The `onerror` is what causes this issue.

```javascript
    onerror(err) {
        // don't do anything if there is no error.
        // this allows you to pass `this.onerror`
        // to node-style callbacks.
        if (null == err) return;
    
        if (!(err instanceof Error)) err = new Error(util.format('non-error thrown: %j', err));
    
        let headerSent = false;
        if (this.headerSent || !this.writable) {
          headerSent = err.headerSent = true;
        }
    
        // delegate
        this.app.emit('error', err, this); // <~ first here
    
        // nothing we can do here other
        // than delegate to the app-level
        // handler and log.
        if (headerSent) {
          return;
        }
    
        const { res } = this;
    
        // first unset all headers
        if (typeof res.getHeaderNames === 'function') { // <~ then here
          res.getHeaderNames().forEach(name => res.removeHeader(name));
        } else {
          res._headers = {}; // Node < 7.7
        }
    
        // then set those specified
        this.set(err.headers);
    
        // force text/plain
            this.type = 'text';
    
        // ENOENT support
        if ('ENOENT' == err.code) err.status = 404;
    
        // default to 500
        if ('number' != typeof err.status || !statuses[err.status]) err.status = 500;
    
        // respond
        const code = statuses[err.status];
        const msg = err.expose ? err.message : code;
        this.status = err.status;
        this.length = Buffer.byteLength(msg);
        this.res.end(msg);
      }
    };
```

What can I do?