Error [ERR_STREAM_WRITE_AFTER_END]: write after end ExpressJS - node.js

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'
}

Related

EPROTO when trying to sock.end in 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.

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!

Discord js keyv throwing ECONNREFUSED

I'm trying to use Keyv, following this tutorial: https://discordjs.guide/keyv/#command-handler and I'm using the command handler code as well.
In my index.js, I run something like this: (with the appropriate imports for Keyv of course)
const Keyv = require('keyv');
const mykeyv = new Keyv('redis://user:pass#localhost:6379');
client.on('message', message =>{
...//skipping some command handler stuff
command.execute(message, args, mykeyv)
})
And then when I run this, I initially get the correct and expected client.on('ready') messages, but then a few seconds after the following error pops up:
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
Emitted 'error' event on Keyv instance at:
at KeyvRedis.<anonymous> (C:\Users\Meroper80\Documents\GitHub\DndBot\Node-Js-Discord-Bot\node_modules\keyv\src\index.js:43:44)
at KeyvRedis.emit (events.js:315:20)
at Redis.<anonymous> (C:\Users\Meroper80\Documents\GitHub\DndBot\Node-Js-Discord-Bot\node_modules\#keyv\redis\src\index.js:17:38)
at Redis.emit (events.js:315:20)
at Redis.silentEmit (C:\Users\Meroper80\Documents\GitHub\DndBot\Node-Js-Discord-Bot\node_modules\ioredis\built\redis\index.js:499:26)
at Socket.<anonymous> (C:\Users\Meroper80\Documents\GitHub\DndBot\Node-Js-Discord-Bot\node_modules\ioredis\built\redis\event_handler.js:186:14)
at Object.onceWrapper (events.js:422:26)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
I'm not sure what this error is about, can someone help me?

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')
})

MongoNetworkError: connection 5 to ercluster-shard-00-01-8xhj2.mongodb.net:27017 timed out

I am using react and nodejs.When i send query to mongodb , after some time (2-3 min) i am geting this error(MongoNetworkError timed out). I am using cluster that i created on mongo atlas.
I tried to create new account and cluster on mongo atlas , set up new mongo uri ,but the problem is same.
console.log("mongoUri:", config.mongoUri);
const options = {
useNewUrlParser: true
// keepAlive: 300000,
// connectTimeoutMS: 30000
};
mongoose.connect(config.mongoUri, options, err => {
console.log("error occured", err);
});
console.log("mongoose connection:", mongoose.connection.readyState);
mongoose.connection.readyState;
error occured { MongoNetworkError: connection 4 to etcluster-shard-00-01-u7zep.mongodb.net:27017 timed
out
at TLSSocket.<anonymous> (C:\Users\Petar\Documents\GitHub\eliteranks\elite-ranks-back\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:335:7)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at TLSSocket.Socket._onTimeout (net.js:449:8)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
events.js:167
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27)
Emitted 'error' event at:
at TLSSocket.<anonymous> (C:\Users\Petar\Documents\GitHub\eliteranks\elite-ranks-back\node_modules\mongoose\node_modules\mongodb-core\lib\connection\connection.js:321:10)
at Object.onceWrapper (events.js:273:13)
at TLSSocket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
[nodemon] app crashed - waiting for file changes before starting...

Resources