客户端收到服务器的“错误请求”响应

我正在使用nodejs网络库。 在本地,一切正常,我可以使用客户端连接到服务器,将数据发送到服务器,然后得到响应。

但是一旦部署在服务器上(使用traefik),当我运行client-app.js时,我会不断得到:

Connected
Received: HTTP/1.1 400 Bad Request
Content-Type: text/plain; charset=utf-8
Connection: close

400 Bad Request
Connection closed

Traefik配置为将到达“ my-address.com”的每个请求重定向到Docker容器(运行server-app.js)上的1337端口。

这是我的代码:

server-app.js:

const net = require('net');

const PORT = 1337;
const HOST = '0.0.0.0';

var server = net.createServer(function(socket) {
    socket.write('Echo server\r\n');
    //socket.pipe(socket);

    socket.on('data',(data) => {
        console.log('DATA RECEIVED')
        socket.write('GOT DATA',data)

    });

});

server.on('connection',(socket)=> {
    console.log('Connection from: ',socket.remoteAddress)
});

server.listen(PORT,HOST,() => {
    console.log(`SERVER IS UP. PORT ${PORT}`)
})

client-app.js:

const net = require('net');


const PORT = 443;
const HOST = 'my-addres.com';

var client = new net.Socket();
console.dir(client.connect)
client.connect({port: PORT,host: HOST},function() {
    console.log('Connected');
    client.write('Hello,server! Love,Client.\n');
    var counter = 1;

    setInterval(()=>{client.write(`Data nr ${counter}\n`); counter += 1},1000)
});

client.on('data',function(data) {
    console.log('Received: ' + data);
    //client.destroy(); // kill client after server's response
});

client.on('close',function() {
    console.log('Connection closed');
});
mingdou 回答:客户端收到服务器的“错误请求”响应

您的客户端和服务器不会使用HTTP协议,但是会在TCP之上执行自己的应用程序协议。但是由于您收到的是HTTP响应,因此您似乎已将Traefik配置为HTTP路由器。由于您不使用HTTP,因此不应使用HTTP,而应使用TCP router

本文链接:https://www.f2er.com/3072257.html

大家都在问