NodeJS - Сервер сбрасывает соединение
Всем добрый день! Помогите пожалуйста, не могу управиться со следующей ошибкой, что выводит консоль:
Server running at http://127.0.0.1:8124/
connected to server
HTTP/1.1 400 Bad Request
Connection: close
connection is closed
Error: This socket has been ended by the other party
at Socket.writeAfterFIN [as write] (net.js:454:14)
at ReadStream.<anonymous> (C:\Users\cann\Desktop\Data\nodejs\3\hello.js:19:11)
at ReadStream.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at ReadStream.Readable.push (_stream_readable.js:212:10)
at TTY.onStreamRead (internal/stream_base_commons.js:186:23) {
code: 'EPIPE'
}
Код моей программы:
var http = require('http')
var net = require('net')
var client = new net.Socket()
client.setEncoding('utf-8')
// Создаём наш сервер
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end()
}).listen(8124)
// Подключение к серверу
client.connect ('8124','localhost', function () {
console.log('connected to server');
client.write('Who needs a browser to communicate?');
});
// Полученные данные отправляются серверу
process.stdin.on('data', function (data) {
client.write(data);
});
// Возвращаемые данные выводятся на консоль
client.on('data',function(data) {
console.log(data);
});
// При закрытии сервера
client.on('close', function() {
console.log('connection is closed');
});
client.on('error', function (err) {
console.error(err)
})
console.log('Server running at http://127.0.0.1:8124/')