EPROTO when trying to sock.end in Node.js - node.js

I am trying to create a simple node server and call it. Here is what I am doing:
var net = require('net');
const https = require('https');
var srv = net.createServer(function(sock) {
sock.end('Hello world\n');
});
srv.listen(0, function() {
console.log('Listening on port ' + srv.address().port);
https.get('https://localhost:' + srv.address().port);
});
Here is the error I am getting:
Listening on port 35147
events.js:167
throw er; // Unhandled 'error' event
^
Error: write EPROTO 140575534664576:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:252:
at WriteWrap.afterWrite [as oncomplete] (net.js:833:14)
Emitted 'error' event at:
at TLSSocket.socketErrorListener (_http_client.js:382:9)
at TLSSocket.emit (events.js:182:13)
at onwriteError (_stream_writable.js:431:12)
at onwrite (_stream_writable.js:456:5)
at _destroy (internal/streams/destroy.js:40:7)at TLSSocket.Socket._destroy (net.js:603:3)
at TLSSocket.destroy (internal/streams/destroy.js:32:8)
at WriteWrap.afterWrite [as oncomplete] (net.js:835:10)
I am trying to run that code here: https://www.tutorialspoint.com/execute_nodejs_online.php.
How could I make the sock.end or is there an easier way to create a node server listener? I.e. without using the sockets.

Related

Error [ERR_STREAM_WRITE_AFTER_END]: write after end ExpressJS

I forked a repo I found and running it locally was fine with no errors however I wanted to run it locally on https so I changed the code of src/index.js
const server = createServer();
to
const options = {
key: fs.readFileSync('localhost-key.pem'),
cert: fs.readFileSync('localhost.pem')
};
const server = https.createServer(options, app)
But I started getting the error after the change when I try to access the website on https://localhost:8080
Full Error:
node:events:491
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:399:5)
at write_ (node:_http_outgoing:868:11)
at ServerResponse.write (node:_http_outgoing:827:15)
at ReadStream.ondata (node:internal/streams/readable:766:22)
at ReadStream.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at node:internal/fs/streams:275:14
at FSReqCallback.wrapper [as oncomplete] (node:fs:681:5)
Emitted 'error' event on ServerResponse instance at:
at ServerResponse.onerror (node:internal/streams/readable:785:14)
at ServerResponse.emit (node:events:525:35)
at emitErrorNt (node:_http_outgoing:846:9)
at process.processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}

How to connect a remote server from client by http.get of Node.js?

I'm using node.js to create connect to server on EC2.
The client.js is on the physical machine using a function http.get to get to the server.js on EC2, which using the express.get to connect.
When I ran the two file on the EC2 and my physical machine, nothing happened. And I had the error of Error: connect ETIMEDOUT.
Am I using the wrong ip address? What I use is the public Ipv4 ip.
server.js on EC2 with Ipv4 add: 100.27.18.131
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
calc = 0
for (i=0;i<10000;i++) {
calc += Math.random() * Math.random();
}
console.log(calc);
res.send(calc.toFixed(10));
});
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
client.js on pyhsical machine:
const http = require('http');
setInterval(loadtest, 100); //time is in ms
function loadtest()
{
http.get('http://100.27.18.131:3000', (res) => {
res.on('data', function (chunk) {
console.log('' + chunk);
});
});
}
Error:
node:events:505
throw er; // Unhandled 'error' event
^
Error: connect ETIMEDOUT 100.27.18.131:3000
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1187:16)
Emitted 'error' event on ClientRequest instance at:
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4039,
code: 'ETIMEDOUT',
syscall: 'connect',
address: '100.27.18.131',
port: 3000
}
Did not set up the port for 3000 or 80 at the vary beginning.

Error: connect ECONNREFUSED 127.0.0.1:6379 at TCPConnectWrap.afterConnect [as oncomplete]

I have run the server and getting an error as:
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on Queue instance at:
at RedisClient.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\kue\lib\redis.js:65:13)
at RedisClient.emit (events.js:315:20)
at RedisClient.on_error (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:401:14)
at Socket.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:279:14)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
My Code is as:
const env = require('./environment');
mongoose.connect(`mongodb://localhost/${env.db}`);
const db = mongoose.connection;
db.on('error', console.error.bind(console, "Error connecting to MongoDB"));
db.once('open', function(){
console.log('Connected to Database :: MongoDB');
});
module.exports = db;
I have tried by installing redis and adding it into script but error is same.And also tried in production and development environment but the error is same.
This issue seems to be with redis server. Try running redis-server in the terminal.
Hope this helps!

access denied :::7000 in nodejs event 498

I trying to listen on port using express:
const http=require('http');
const PORT_=process.env.PORT||7000;
const express=require('express');
const app=express();
app.use((req,res,next)=>{
res.send("Hellow world"+PORT_);
next();
});
app.listen(PORT_,(err)=>{
err===true ? console.log(err):console.log('run on '+port_);
});
When I run the program on any port, get following error:
node:events:498
throw er; // Unhandled 'error' event
^
Error: listen EACCES: permission denied :::7000
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1357:8)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'EACCES',
errno: -4092,
syscall: 'listen',
address: '::',
port: 7000
The program works properly while I'm using http module.
You are getting the error because the port you are trying to listen on is already in use. Try to listen on a different Port other than the one you are using in your code.

nodejs http request socket hang up

I've been trying to send http reqeust by http node module by following code
const https = require('https')
http.request('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
but got the message
events.js:174
throw er; // Unhandled 'error' event
^
Error: socket hang up
at createHangUpError (_http_client.js:332:15)
at TLSSocket.socketOnEnd (_http_client.js:435:23)
at TLSSocket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
at TLSSocket.socketOnEnd (_http_client.js:435:9)
at TLSSocket.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1145:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
When I switch the code to
const request = require('request')
request.get('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})
it worked and got a response with 200.
Then I try to capture network packages by Fiddler, I found no package was sent when using http.request.
This error only occurs in Windows system inside our working domain network.
maybe try https instead of http since you defined it https
https.request('http://newportal.timesgroup.cn/Pages/Home.aspx', () => {
console.log('success')
})

Resources