【问题标题】:Client getting 'Bad Request' response from server客户端从服务器获得“错误请求”响应
【发布时间】:2020-03-15 04:21:31
【问题描述】:

我正在使用 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 端口。

这是我的代码:

服务器-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');
});

【问题讨论】:

    标签: node.js sockets traefik


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多