Node server is getting down with Error: write after end - node.js

We are streaming files from s3, its running well but sometime (randomly in few days) its gives error and node instance is getting down. our code is
s3Utils.headObject(assetId, function (err, metadata) {
if (err && err.code === 'NotFound') {
console.log("file not found on s3: ", assetId) ;
res.send({"success":false, "message":"file not found on s3: "+ assetId});
} else {
var download_file_name = assetId;
if(metadata.Metadata && metadata.Metadata.filename) {
download_file_name = metadata.Metadata.filename;
}
if(metadata.ContentLength) {
res.setHeader('Content-Length',metadata.ContentLength);
}
res.setHeader('Content-disposition', 'attachment; filename=' +download_file_name);
res.attachment(download_file_name);
var fileStream = s3Utils.getObjectAsReadStream(assetId);
fileStream.pipe(res);
}
});
we are getting below error in logs
events.js:154
throw er; // Unhandled 'error' event
^
Error: write after end
at writeAfterEnd (_stream_writable.js:166:12)
at PassThrough.Writable.write (_stream_writable.js:211:5)
at IncomingMessage.ondata (_stream_readable.js:536:20)
at emitOne (events.js:90:13)
at IncomingMessage.emit (events.js:182:7)
at IncomingMessage.Readable.read (_stream_readable.js:368:10)
at flow (_stream_readable.js:751:26)
at resume_ (_stream_readable.js:731:3)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
Server is getting down after this error.
Please suggest.

Related

imageMagick fails at socket instance

I'm using the imageMagick subclass of graphicsmagick to alter ~7k png images. The function below gets about 2/3 of the way through and fails (see log).
Seems to be an issue where a socket is failing. How do I debug this?
A thought I have is to use the -limit method, but no dice...
Thanks in advance :)
Code:
// changing an image's resolution is the same thing as changing its DPI
export default async function changeScreenshotResolution(dimension) {
new Promise(() => {
try {
const screenshots = fs.readdirSync(path.join(__dirname, `../output/screenshots/${dimension}`), function(error) {if (error) console.log(error)});
screenshots.map(screenshot => {
try {
let readStream = fs.createReadStream(path.join(__dirname, `../output/screenshots/${dimension}/${screenshot}`));
let writeStream = fs.createWriteStream(path.join(__dirname, `../output/screenshots/${dimension}1/${screenshot}`));
im(readStream, screenshot)
.limit('memory', '32MB') // i tried variations of this but it made no difference
.units('PixelsPerInch')
.density(300,300)
.stream()
.pipe(writeStream)
console.log(screenshot);
} catch (error) {
console.log(`Error changing screenshot resolution: ${error}`);
}
})
} catch (error) {
console.log(`changeScreenshotResolution error: ${error}`);
}
})
};
Error: spawn convert EAGAIN
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:468:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -35,
code: 'EAGAIN',
syscall: 'spawn convert',
path: 'convert',
spawnargs: [
'-density',
'300x300',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect/6550 ARABIAN CIR.png',
'-units',
'PixelsPerInch',
'/Users/daddy/Dev/scout/project-sunroof-scraping/output/screenshots/rect1/6550 ARABIAN CIR.png'
]
}
events.js:292
throw er; // Unhandled 'error' event
^
Error: read ENOTCONN
at tryReadStart (net.js:571:20)
at Socket._read (net.js:582:5)
at Socket.Readable.read (_stream_readable.js:474:10)
at Socket.read (net.js:622:39)
at new Socket (net.js:374:12)
at Object.Socket (net.js:265:41)
at createSocket (internal/child_process.js:313:14)
at ChildProcess.spawn (internal/child_process.js:436:23)
at Object.spawn (child_process.js:548:9)
at spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/cross-spawn/index.js:17:18)
at gm._spawn (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:224:14)
at /Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:101:12
at series (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/array-series/index.js:11:36)
at gm._preprocess (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:177:5)
at gm.write (/Users/daddy/Dev/scout/project-sunroof-scraping/node_modules/gm/lib/command.js:99:10)
at /Users/daddy/Dev/scout/project-sunroof-scraping/modules/changeScreenshotDPI.js:18:26
Emitted 'error' event on Socket instance at:
at emitErrorNT (internal/streams/destroy.js:100:8)
at emitErrorCloseNT (internal/streams/destroy.js:68:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: -57,
code: 'ENOTCONN',
syscall: 'read'
}

Alternative to waiting for child_process to quit node.js

I've got these 2 codes running in 1 script:
The ping script:
app.get("/ping", function(req, res) {
res.send("Pong!");
});
And a work in progress youtube downloader:
app.post("/nodedl", function(req, res) {
res.write("===Gradyncore listnener===\n")
res.write("Recived POST request from "+req.ip+" to /nodedl\n")
res.write("POST(url): "+req.body.url+"\n")
res.write("checking key...\n")
if (req.body.key==="<Insert key here>"){
res.write("Key is valid! Skipping pre-download script...\n")
} else {
res.write("Key is invalid. Running pre-download script...\n")
exec("/home/gradyn/website/projects/nodeDL/scripts/check.sh", function (error, results) {
if (results != null) {
res.write(results+"\n");
} else if (error != null) {
res.write("Error: " + error+"\n");
}
});
}
res.end();
});
The problem is, by the time check.sh finishes, res.end(); has allredy been emitted, causing this error followed by a crash
events.js:160
throw er; // Unhandled 'error' event
^
Error: write after end
at ServerResponse.OutgoingMessage.write (_http_outgoing.js:439:15)
at /home/gradyn/listener/app.js:29:13
at ChildProcess.exithandler (child_process.js:213:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Pipe._handle.close [as _onclose] (net.js:498:12)
I'm fairly new to node.js, but from what I understand, If i wait for the child process to complete before calling res.end(), the entire script (including the ping listener) will not work until the child process completes.
How should I go about doing this?
Problem is that your exec is asynchronous, code below it won't wait to for completion of exec and get executed... You have to end response inside callback function:
app.post("/nodedl", function(req, res) {
res.write("===Gradyncore listnener===\n")
res.write("Recived POST request from "+req.ip+" to /nodedl\n")
res.write("POST(url): "+req.body.url+"\n")
res.write("checking key...\n")
if (req.body.key==="<Insert key here>"){
res.write("Key is valid! Skipping pre-download script...\n")
} else {
res.write("Key is invalid. Running pre-download script...\n")
exec("/home/gradyn/website/projects/nodeDL/scripts/check.sh", function(error, results) {
if (results != null) {
res.write(results+"\n");
res.end();
} else if (error != null) {
res.write("Error: " + error+"\n");
res.end();
}
});
}
});

Get audio file duration and size in node js

I am using musicmetadata module of node js to read duration and size but it is not working for all mp3 files.getting below error:-
Error: Could not find metadata header
at /var/www/html/Live/node_modules/musicmetadata/lib/id3v1.js:13:19
at Stream.done (/var/www/html/Live/node_modules/musicmetadata/lib/common.js:31:5)
at emitNone (events.js:91:20)
at Stream.emit (events.js:185:7)
at drain
(/var/www/html/Live/node_modules/musicmetadata/node_modules/through/index.js:34:23)
at Stream.stream.queue.stream.push (/var/www/html/Live/node_modules/musicmetadata/node_modules/through/index.js:45:5)
at Stream.end (/var/www/html/Live/node_modules/musicmetadata/node_modules/through/index.js:15:35)
at _end (/var/www/html/Live/node_modules/musicmetadata/node_modules/through/index.js:65:9)
at Stream.stream.end (/var/www/html/Live/node_modules/musicmetadata/node_modules/through/index.js:74:5)
at ReadStream.onend (_stream_readable.js:512:10)
at ReadStream.g (events.js:286:16)
at emitNone (events.js:91:20)
at ReadStream.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickDomainCallback (internal/process/next_tick.js:122:9)
+ Error: NODE_ENV is not defined! Using default development environment
code example:it contain required code to understand issue.
var fs = require('fs');
var musicData = require('musicmetadata');
//working for "01 .Brown Rang -Mp3 Songs -[320kbps]-[Exclusive]~~~[CooL GuY] {{a2zRG}}.mp3"
var parser = musicData(fs.createReadStream('03 RANG DE CHUNRIYA.mp3'),{ duration: true }, function (err, metadata) {
var talkItem = {};
if (err)
{
console.log('err:',err);
}
else{
if(metadata.title === undefined || metadata.title === "")
{
talkItem.title ='';
}
else{
talkItem.title = metadata.title;
}
talkItem.duration = metadata.duration;
console.log('talkItem:',talkItem);
}
});
you can see code mp3 file here

throw new RangeError node js

I'm trying to work with IBM Watson Conversation service with Node.js.
I use 'express' to post a message:
app.post( '/api/message', function(req, res) {
}
and to print the message got from the service:
conversation.message( payload, function(err, data) {
if ( err ) {
return res.status( err.code || 500 ).json( err );
}
return res.json( updateMessage( payload, data ) );
} );
I just ran the application on port 3000. While the page is not loaded and I got this error:
_http_server.js:192
throw new RangeError(`Invalid status code: ${statusCode}`);
^
RangeError: Invalid status code: 0
at ServerResponse.writeHead (_http_server.js:192:11)
at ServerResponse._implicitHeader (_http_server.js:157:8)
at ServerResponse.OutgoingMessage.end (_http_outgoing.js:573:10)
at ServerResponse.send (C:\IBM\1.Mission\2016\conversation-simple-master(1)\
conversation-simple-master\node_modules\express\lib\response.js:204:10)
at ServerResponse.json (C:\IBM\1.Mission\2016\conversation-simple-master(1)\
conversation-simple-master\node_modules\express\lib\response.js:249:15)
at C:\IBM\1.Mission\2016\conversation-simple-master(1)\conversation-simple-m
aster\app.js:86:44
at Request._callback (C:\IBM\1.Mission\2016\conversation-simple-master(1)\co
nversation-simple-master\node_modules\watson-developer-cloud\lib\requestwrapper.
js:47:7)
at self.callback (C:\IBM\1.Mission\2016\conversation-simple-master(1)\conver
sation-simple-master\node_modules\watson-developer-cloud\node_modules\request\re
quest.js:200:22)
at emitOne (events.js:77:13)
at Request.emit (events.js:169:7)
I don't think the problem is from npm, back my package... While it seems a generic problem...Thanks for you help.
Request to IBM Watson Conversation service probably ended with error with code "0" and it isn't a valid HTTP status code. This should work:
conversation.message(payload, function(err, data) {
if (err) {
return res.status(500).json(err);
}
return res.json(updateMessage(payload, data));
});

Node.js Error: Max redirects exceeded

how I can ignore pages with cycle redirects?
I use this code to fetching pages:
var libxml = require("libxmljs"),
http = require('follow-redirects').http,
url = require("url");
var request = http.request( { "host": host, "path": URL, "port": 80 }, function( response ) {
var str = '';
response.on( 'data', function( chunk ) {
str += chunk;
});
response.on( 'end', function() {
callback( str, response.statusCode );
}).on( 'error', function ( err ) {
console.log( err );
});
}).end();
It will not go to 'error' block, and I've got an exception:
events.js:85
throw er; // Unhandled 'error' event
^
Error: Max redirects exceeded.
at ClientRequest.cb (/var/parsing/node_modules/follow-redirects/create.js:55:19)
at ClientRequest.g (events.js:199:16)
at ClientRequest.emit (events.js:107:17)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:426:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
at Socket.socketOnData (_http_client.js:317:20)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
The error is being thrown by the request object, not the response object, so you need to add an (additional) error listener to request;
var request = http.request(...).on('error', function(err) {
...
}).end();
Looking at the docs for the package you are using (https://www.npmjs.com/package/follow-redirects), it looks like it just has a maxRedirects option. Directly from the linked page:
require('follow-redirects').maxRedirects = 10; // Has global affect (be careful!)
https.request({
host: 'bitly.com',
path: '/UHfDGO',
maxRedirects: 3 // per request setting
}, function (res) {/* ... */});

Resources