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')
})
Related
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'
}
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.
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.
I have a very strange error happening.
I am running Node.js version 8.0.0 on Mac OS X Sierra.
I create a very simple http server that basically returns 'ok' to any request after 8 seconds in this example.
I then use the 'request' package to make a request to my http server every 9 seconds, and use the parameter 'forever' to keep the http session alive.
One request out of two works perfectly fine while the other out of two returns a 'socket hang up' error.
Here is the very simple code to reproduce the issue:
const http = require('http');
const request = require('request');
const port = 3000;
const server = http.createServer((req, res) => {
console.log('request');
setTimeout(() => {
res.end('ok');
}, 8000);
});
server.listen(port);
setInterval(() => {
request(`http://localhost:${port}/`, {forever: true}, (error, response, body) => {
if (error) return console.log('error', error);
console.log('done', body);
});
}, 9000);
When I run this code I get the following output on my Macbook Pro :
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
request
done ok
request
error { Error: socket hang up
at createHangUpError (_http_client.js:343:15)
at Socket.socketOnEnd (_http_client.js:435:23)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9) code: 'ECONNRESET' }
So I get an 'socket hang up' error exactly every other request.
It seems that the error does not seem to appear on an Ubuntu VPS though.
Any ideas as to why this issue is happening ?
It processes most of the records from the .csv file. However, at the end I get this error with csv-parse node module and can't figure out why. It terminates my server so I need to resolve it. Please help
events.js:160
throw er; // Unhandled 'error' event
^
Error: Number of columns on line 37052 does not match header
at Error (native)
at Parser.__push (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:222:13)
at Parser._flush (/Users/Development/apps/SailsJS/my-project/node_modules/csv-parse/lib/index.js:189:12)
at Parser.<anonymous> (_stream_transform.js:118:12)
at Parser.g (events.js:291:16)
at emitNone (events.js:86:13)
at Parser.emit (events.js:185:7)
at prefinish (_stream_writable.js:504:12)
at finishMaybe (_stream_writable.js:512:7)
at endWritable (_stream_writable.js:524:3)
at Parser.Writable.end (_stream_writable.js:489:5)
at ReadStream.onend (_stream_readable.js:511:10)
at ReadStream.g (events.js:291:16)
at emitNone (events.js:91:20)
at ReadStream.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
My Controller API
upload: function(req, res){
req.file('statData').upload(function(error, files){
var fs = require('fs');
var parse = require('csv-parse');
var csvData=[];
var lines = 0;
var filePath = sails.config.appPath + '/data/file.csv';
fs.createReadStream(filePath)
.pipe(parse({
columns: false,
delimiter: '\t',
skip_empty_lines: true,
relax: true
}))
.on('data', function(csvrow) {
csvData.push(csvrow);
})
.on('end',function() {
//do something wiht csvData
});
}
I switched to using fast-csv module and worked.