nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.8k stars 7.65k forks source link

@Req/@Request pipe not working #654

Closed hiagodotme closed 6 years ago

hiagodotme commented 6 years ago

I'm submitting a...


[ ] Regression
[X] Bug report
[ ] Feature request
[ ] Documentation issue or request

I created a proxy with the nestjs. Any request other than GET does not work.

StackOverflow Answer: https://stackoverflow.com/questions/50236234/create-proxy-in-nestjs

Code:

import { IncomingMessage } from 'http';
import { HttpUtils } from './../utils/http.utils';
import { Controller, Param, Req, Res, All } from '@nestjs/common';
import { Config } from './../system/config';
import { ServerResponse } from 'http';
import * as http from 'http';
import { CouchDB, Log } from 'system';
import { UsuarioModel } from 'model';

@Controller('__api-proxy')
export class ApiProxyController {

    @All('*')
    root(@Param() param, @Req() c_req: IncomingMessage, @Res() c_res: ServerResponse): any {

        // habilito o cors
        HttpUtils.enableCors(c_req, c_res);

        const urlDetail = HttpUtils.getUrlDetail(Config.getConfig().endpoints.webapi);

        // modifico para o hostname em que eu estou fazendo o proxy
        c_req.headers.host = urlDetail.hostname;

        // faço o proxy
        var proxy = http.request({
            hostname: urlDetail.hostname,
            port: urlDetail.port,
            method: c_req.method,
            headers: c_req.headers,
            path: '/' + param[0],
        }, (res) => {
            res.pause();
            res.headers.Server = `Sync Server ${Config.getConfig().package.version}`;

            if (Config.getConfig().endpoints.log) {
                Log.print('[ api ] [' + c_req.connection.remoteAddress + ']     : ' + c_req.method + ' ' + res.statusCode + ' ' + c_req.url);
            }

            c_res.writeHead(res.statusCode, res.headers);
            res.pipe(c_res, {end: true});
            res.resume();
        });

        c_req.pipe(proxy, {end: true});
    }
}

NodeJS Error

Error: socket hang up
    at createHangUpError (_http_client.js:331:15)
    at Socket.socketOnEnd (_http_client.js:423:23)
    at emitNone (events.js:111:20)
    at Socket.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9)
kamilmysliwiec commented 6 years ago

Honestly, I don't think that it is a Nest related issue. Besides, if you want to create a proxy and nothing else, you probably don't need Nest at all.

hiagodotme commented 6 years ago

Hello, @kamilmysliwiec First of all, thank you for your response. In my case, it is necessary to make use of the Proxy and still have NestJS. One advantage of doing this is to use the same HTTP port, since I need to do some implementations on nodejs.

I agree with you, thinking about it, this is not NestJS's problem. But I found a solution that served me very well. I'll share if anyone needs something like that.

hiagodotme commented 6 years ago

To solve the problem:

In main.ts right after my const app = await NestFactory.create (AppModule); I have added the following lines:

app.use ('__bank-agency-gateway', proxy(config.endpoints.bankAgencyGateway));
app.use ('__bank-account', proxy(config.endpoints.bankAcountCenter));
app.use ('__bank-safebox', proxy(config.endpoints.bankSafebox));
app.use ('__visa-proxy', proxy(config.endpoints.visaProxy));

And I used the express-http-proxy proxy library ie the requests that are not directed to my proxys NestJS can act.

Thank you

lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.